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



我使用sbt0.13和sbt发布插件来自动增加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,以便提供生成的包的名称。

我使用sbt0.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当您需要ThisBuild配置中version设置的值时,请使用(version in ThisBuild).value

给定以下版本.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
  }
}

sbtshell给了我以下输出:

[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(是将applicationVersion传递给version,因为您在version.sbt中设置了版本,所以它已经由sbt自己设置了。但是,如果您想明确地执行此操作,请执行以下操作:

version := (version in ThisBuild).value

在CCD_ 19中。

[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

相关内容

  • 没有找到相关文章

最新更新