从scala源访问SBT设置

时间:2014-12-29 14:07:08

标签: scala sbt

我想知道是否可以从主scala源读取设置键的值。

例如,我的build.sbt包含:

name := "hello"

version := "0.1"

我想在我的scala源文件(version)中读取namesrc/main/scala/*.scala的值。这可能吗?

1 个答案:

答案 0 :(得分:4)

你需要sbt-buildinfo(https://github.com/sbt/sbt-buildinfo)插件

buildInfoSettings

sourceGenerators in Compile <+= buildInfo

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

buildInfoPackage := "hello"

它将生成包含您需要的所有属性的scala文件,您可以从scala源访问它们

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"

  .....