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?
A setting is something which can take the values stored at other Keys in the build state, and generates a new value for a particular build key. sbt converts all registered Setting[_] objects into a giant linear sequence and compiles them into a task graph. This task graph is then used to execute your build.
All new SBT versions (after 0.7. x ) by default put the downloaded JARS into the . ivy2 directory in your home directory. If you are using Linux, this is usually /home/<username>/.
sbt's Scala version sbt needs Scala jars to run itself since it is written in Scala. sbt uses that same version of Scala to compile the build definitions that you write for your project because they use sbt APIs.
Having said that, at the very beginning, sbt will always download its own internal dependencies, Scala including. It is then saved in ~/. sbt/boot . p.s. The versions of Scala used by sbt internally are not at all different from the ones you may have downloaded already for your Scala/sbt projects.
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"
.....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With