Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing SBT settings from scala source

Tags:

scala

sbt

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?

like image 800
cinsk Avatar asked Dec 29 '14 14:12

cinsk


People also ask

What are sbt settings?

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.

Where are sbt dependencies stored?

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>/.

Does Scala include sbt?

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.

Where does sbt download Scala?

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.


Video Answer


1 Answers

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"

  .....
like image 55
Eugene Zhulenev Avatar answered Oct 12 '22 22:10

Eugene Zhulenev