How to share version values between project/plugins.sbt and project/Build.scala?

sbt-buildinfo

If you need to share version number between build.sbt and hello.scala, what would you normally do? I don’t know about you, but I would use sbt-buildinfo that I wrote.

This can be configured using buildInfoKeys setting to expose arbitrary key values like version or some custom String value. I understand this is not exactly what you’re asking but bear with me.

meta-build (turtles all the way down)

As Jacek noted and stated in Getting Started Guide, the build in sbt is a project defined in the build located in project directory one level down. To distinguish the builds, let’s define the normal build as the proper build, and the build that defines the proper build as meta-build. For example, we can say that an sbt plugin is a library of the root project in the meta build.

Now let’s get back to your question. How can we share info between project/Build.scala and project/plugins.sbt?

using sbt-buildinfo for meta-build

We can just define another level of build by creating project/project and add sbt-buildinfo to the (meta-)meta-build.

Here are the files.

In project/project/buildinfo.sbt:

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.3.2")

In project/project/Dependencies.scala:

package metabuild

object Dependencies {
  def scalaJsVersion = "0.5.0-M2"
}

In project/build.properties:

sbt.version=0.13.5

In project/buildinfo.sbt:

import metabuild.Dependencies._

buildInfoSettings

sourceGenerators in Compile <+= buildInfo

buildInfoKeys := Seq[BuildInfoKey]("scalaJsVersion" -> scalaJsVersion)

buildInfoPackage := "metabuild"

In project/scalajs.sbt:

import metabuild.Dependencies._

addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % scalaJsVersion)

In project/Build.scala:

import sbt._
import Keys._
import metabuild.BuildInfo._

object Builds extends Build {
  println(s"test: $scalaJsVersion")
}

So there’s a bit of a boilerplate in project/buildinfo.sbt, but the version info is shared across the build definition and the plugin declaration.

If you’re curious where BuildInfo is defined, peek into project/target/scala-2.10/sbt-0.13/src_managed/.

Leave a Comment