cinsk
cinsk

Reputation: 1716

accessing SBT settings from scala source

I'm wondering if it is possible to read the value of setting key from the main scala sources.

For example, my build.sbt contains:

name := "hello"

version := "0.1"

I want to read the value of version and name in my scala source files (in src/main/scala/*.scala). Is this possible?

Upvotes: 6

Views: 1361

Answers (1)

Eugene Zhulenev
Eugene Zhulenev

Reputation: 9734

You need sbt-buildinfo (https://github.com/sbt/sbt-buildinfo) plugin for it

buildInfoSettings

sourceGenerators in Compile <+= buildInfo

buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion)

buildInfoPackage := "hello"

it will generate scala file with all properties you need, and you can access them from your scala source

package hello

/** This object was generated by sbt-buildinfo. */
case object BuildInfo {
  /** The value is "helloworld". */
  val name = "helloworld"
  /** The value is "0.1-SNAPSHOT". */
  val version = "0.1-SNAPSHOT"
  /** The value is "2.10.3". */
  val scalaVersion = "2.10.3"

  .....

Upvotes: 5

Related Questions