2

我使用 sbt 0.13sbt-release插件来自动增加 P​​lay 应用程序的版本,所以我有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)

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

4

1 回答 1

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函数的值):

[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(参见GitHub 上的 play.Project.scala)是传递applicationVersionversion并且因为您在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
于 2014-03-20T22:56:05.297 回答