Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional addSbtPlugin based on scalaVersion

Tags:

sbt

I'm using a plugin (sbt-scapegoat) which only works for Scala 2.11.

Can I have a conditional addSbtPlugin based on scalaVersion? Like:

if (scalaVersion.value.startsWith("2.11")) addSbtPlugin("com.sksamuel.scapegoat" %% "sbt-scapegoat" % "0.94.6")

How can I do this in SBT?

Jianshi

like image 515
huangjs Avatar asked Apr 23 '15 06:04

huangjs


2 Answers

tl;dr It's not possible given the description of the problem.

There are at least two build configurations involved in a sbt project - the real project (you want to bet your money on) and the meta build for the build of your project. Yes, I know it sounds a little weird, but it's a very powerful concept IMHO.

See sbt is recursive:

The project directory is another build inside your build, which knows how to build your build. To distinguish the builds, we sometimes use the term proper build to refer to your build, and meta-build to refer to the build in project. The projects inside the metabuild can do anything any other project can do. Your build definition is an sbt project.

sbt runs atop Scala and requires a strict version of it. No way to change it unless you fancy spending time on things you should really not be touching in the first place :)

What you can do is to apply the plugin in project/plugins.sbt and then, in the project, apply the settings of the plugin selectively, per scalaVersion of the project's build not the meta-build's itself.

It's not that complicated as the answer reads, but explaining simple concepts is usually not an easy task for me. Have fun with sbt! It's gonna pay you back very soon when used properly.

like image 138
Jacek Laskowski Avatar answered Nov 14 '22 12:11

Jacek Laskowski


Updated answer for 2020: You can use .filter on addSbtPlugin.

For example, the following works:

val scalafixEnabled = System.getProperty("SCALAFIX", "").trim.nonEmpty

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.14").filter(_ => scalafixEnabled)
like image 35
Golly Avatar answered Nov 14 '22 13:11

Golly