I know this because build.sbt knows this.
sbt-buildinfo generates Scala source from your build definitions.
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.2.5")
Add the following in your build.sbt
:
buildInfoSettings
sourceGenerators in Compile <+= buildInfo
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion)
buildInfoPackage := "hello"
(Note: in version 0.1.2, this was Seq[Scoped]
instead!)
Alternatively, if you are using Build.scala
, add the import import sbtbuildinfo.Plugin._
to it and add the module's setting through something like:
lazy val myProject = Project(
id = "myProjectName",
base = file("."),
settings = Defaults.defaultSettings ++
buildInfoSettings ++
Seq(
sourceGenerators in Compile <+= buildInfo,
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion),
buildInfoPackage := "org.myorg.myapp"
) ++
…
)
When you reload the settings and compile, this generates the following:
package hello
object BuildInfo {
val name = "helloworld"
val version = "0.1-SNAPSHOT"
val scalaVersion = "2.9.2"
val sbtVersion = "0.12.0"
}
Customize buildInfoKeys
by adding whatever keys. You can use BuildInfoKey.map
to change the generated field
name and value, add new fields with tuples, or add new fields with values computed at build-time:
buildInfoKeys ++= Seq[BuildInfoKey](
resolvers,
libraryDependencies in Test,
BuildInfoKey.map(name) { case (k, v) => "project" + k.capitalize -> v.capitalize },
"custom" -> 1234, // computed at project load time
BuildInfoKey.action("buildTime") {
System.currentTimeMillis
} // re-computed each time at compile
)
This generates:
val resolvers = Seq("Sonatype Public: https://oss.sonatype.org/content/groups/public")
val test_libraryDependencies = Seq("org.scala-lang:scala-library:2.9.1", ...)
val projectName = "Helloworld"
val custom = 1234
val buildTime = 1346906092160L
Tasks can be added only if they do not depend on sourceGenerators
. Otherwise, it will cause an infinite loop.
Here's how to change the generated the object name:
buildInfoObject := "Info"
This changes the generated object to object Info
. Changing the object name is optional, but to avoid name clash with other jars, package name should be unique.
A build number can be generated as follows. Note that cross building against multiple Scala would each generate a new number.
buildInfoKeys += buildInfoBuildNumber
If you use the sbteclipse plugin to generate projects for Eclipse, you need to tell sbteclipse
that the generated BuildInfo.scala
is a managed source, i.e., a generated source file.
To do so, you can configure sbteclipse
as follows:
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Managed
This is explained in more detail in the sbtecliipse
documentation.
MIT License