将version.sbt中定义的ThisBuild设置中的版本值传递给artifactName?

时间:2014-03-20 15:57:29

标签: playframework sbt sbt-release

我使用sbt 0.13 sbt-release插件来自动增加Play应用程序的版本,因此我的文件version.sbt包含以下行:

version in ThisBuild := "1.0-SNAPSHOT"

我还准备了映射工件名称,以便为每个环境准备具有特定名称的包:

val main = play.Project(appName, dependencies = appDependencies).
      settings(
        ...
        artifactName := {(sv: ScalaVersion, module: ModuleID, artifact: Artifact) => {
          val suffix = "-" + sys.props.getOrElse("environment","dev") + (if (artifact.classifier.isDefined) "-" + artifact.classifier.get else "")
          artifact.name + "_" + sv.binary + "-" + version + suffix + "." + artifact.extension
        }},
        ...
      )

评估上面的代码导致版本值等于1.0(可能是某种默认值),因为version.sbt将值定义为1.0-SNAPSHOT 在很多情况下,我会这样做,以便使用ThisBuild中的版本:

SomeTask <<= (version in ThisBuild) map { ver => ...
  some ver usage
  ...
}

但是这个特定的解决方案不起作用。有人可以提供一个如何在artifactName设置中使用此特定值的示例吗?

另一个可以帮助我的解决方案是从此变量中获取字符串值。多亏了它,我将能够做下一件事:

val appVersion := ... // some operation on version in ThisBuild
val main = play.Project(appName, appVersion, dependencies = appDependencies)

我想将version设置的值从version.sbt文件传递到artifactName,以便为生产的包提供名称。

1 个答案:

答案 0 :(得分:3)

我使用sbt 0.13.3-SNAPSHOT

[sbt-0-13-3]> about
[info] This is sbt 0.13.3-SNAPSHOT
[info] The current project is {file:/Users/jacek/sandbox/so/sbt-0.13.3/}sbt-0-13-3 1.0-build_sbt
[info] The current project is built against Scala 2.11.0-RC3
[info] Available Plugins: sbt.plugins.IvyModule, sbt.plugins.JvmModule, sbt.plugins.GlobalModule, com.typesafe.sbt.SbtGit, com.typesafe.sbt.SbtProguard, growl.GrowlingTests, org.sbtidea.SbtIdeaPlugin, sbtman.Plugin, np.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.3

tl; dr 当您需要(version in ThisBuild).value配置中version设置的值时,请使用ThisBuild

鉴于以下 version.sbt 文件:

version in ThisBuild := "1.0-version_sbt"

以及 build.sbt (注意(version in ThisBuild).value):

scalaVersion := "2.11.0-RC3"

version := "1.0-build_sbt"

artifactName := {
  (sv: ScalaVersion, module: ModuleID, artifact: Artifact) => {
    val suffix = "-" + sys.props.getOrElse("environment","dev") + (if (artifact.classifier.isDefined) "-" + artifact.classifier.get else "")
    artifact.name + "_" + sv.binary + "-" + (version in ThisBuild).value + suffix + "." + artifact.extension
  }
}

sbt shell给了我以下输出:

[sbt-0-13-3]> show makePom::artifactPath
[info] /Users/jacek/sandbox/so/sbt-0.13.3/target/scala-2.11.0-RC3/sbt-0-13-3_2.11.0-RC3-1.0-version_sbt-dev.pom

我使用makePom::artifactPath设置,因为我注意到它是artifactName的反向依赖(否则我无法显示artifactName函数的值):< / p>

[sbt-0-13-3]> inspect artifactName
[info] Setting: scala.Function3[sbt.ScalaVersion, sbt.ModuleID, sbt.Artifact, java.lang.String] = <function3>
[info] Description:
[info]  Function that produces the artifact name from its definition.
[info] Provided by:
[info]  {file:/Users/jacek/sandbox/so/sbt-0.13.3/}sbt-0-13-3/*:artifactName
[info] Defined at:
[info]  /Users/jacek/sandbox/so/sbt-0.13.3/build.sbt:5
[info] Dependencies:
[info]  {.}/*:version
[info] Reverse dependencies:
[info]  *:makePom::artifactPath
[info] Delegates:
[info]  *:artifactName
[info]  {.}/*:artifactName
[info]  */*:artifactName
[info] Related:
[info]  */*:artifactName

对于您想要将Play项目的版本设置为version in ThisBuild的情况,您不必担心它,因为sbt会为您执行此操作。

val appVersion := ... // some operation on version in ThisBuild
val main = play.Project(appName, appVersion, dependencies = appDependencies)

play.Project所做的事情(请参阅play.Project.scala on GitHub)是将applicationVersion传递给version,因为您在version.sbt中设置版本已经由sbt本身设置。如果您想明确地执行此操作,请执行以下操作:

version := (version in ThisBuild).value
build.sbt中的

[sbt-0-13-3]> show version
[info] 1.0-build_sbt
[sbt-0-13-3]> reload
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Loading project definition from /Users/jacek/sandbox/so/sbt-0.13.3/project
[info] Set current project to sbt-0-13-3 (in build file:/Users/jacek/sandbox/so/sbt-0.13.3/)
[sbt-0-13-3]> show version
[info] 1.0-version_sbt