【发布时间】:2018-06-05 10:08:48
【问题描述】:
我有一个半复杂的 SBT 流程,因为我需要根据需要的构建类型有条件地包含不同的配置文件。我通过子项目解决了这个问题:
lazy val app = project
.in(file("."))
.enablePlugins(JavaAppPackaging)
.settings(
commonSettings // Seq() of settings to be shared between projects
,sourceGenerators in Compile += (avroScalaGenerateSpecific in Compile).taskValue
,(avroSpecificSourceDirectory in Compile) := new java.io.File("src/main/resources/com/coolCompany/folderName/avro")
)
lazy val localPackage = project
.in(file("build/local"))
.enablePlugins(JavaAppPackaging)
.settings(
organization := "com.coolCompany",
version := "0.1.0-SNAPSHOT",
scalaVersion := "2.11.8",
name := "my-neat-project",
scalacOptions := compilerOptions, //Seq() of compiler flags
sourceDirectory in Compile := (sourceDirectory in (app, Compile)).value,
mappings in Universal += {
((sourceDirectory in Compile).value / "../../conf/local/config.properties") -> "lib/config.properties"
}
)
.dependsOn(app)
val buildNumber = inputKey[String]("The version number of the artifact.")
lazy val deployedPackage = project
.in(file("build/deployed"))
.enablePlugins(JavaAppPackaging)
.settings(
organization := "com.coolCompany",
buildNumber := {
val args : Seq[String] = spaceDelimited("<arg>").parsed
println(s"Input version number is ${args.head}")
args.head
},
version := buildNumber.inputTaskValue + "-SNAPSHOT", //"0.1.0-SNAPSHOT",
scalaVersion := "2.11.8",
name := "my-cool-project",
scalacOptions := compilerOptions,
sourceDirectory in Compile := (sourceDirectory in (app, Compile)).value,
mappings in Universal += {
((sourceDirectory in Compile).value / "../../conf/deployed/config.properties") -> "lib/config.properties"
}
)
.dependsOn(app)
现在我需要在构建时允许构建工具传入版本号。您可以看到我已经尝试做的事情:我创建了一个名为buildNumber 的inputKey 任务,然后尝试在version := 定义中访问它。我可以很好地运行buildNumber 任务本身:
$ sbt 'deployedPackage/buildNumber 0.1.2'
Input version number is 0.1.2
所以我至少可以验证我的输入任务是否按预期工作。问题是,在运行我想要的实际 packageBin 步骤时,我无法弄清楚我实际上是如何获得该输入值的。
我尝试了以下方法:
$ sbt 'deployedPackage/universal:packageBin 0.1.2'
[error] Expected key
[error] Expected '::'
[error] Expected end of input.
[error] deployedPackage/universal:packageBin 0.1.2
所以它显然不明白如何处理版本号。我尝试了一堆不同的输入变体,例如[...]packageBin buildNumber::0.1.2、[...]packageBin -- buildNumber 0.1.2 或[...]packageBin -- 0.1.2,它们都给出了该错误或类似的东西,表明它不理解我要传递的内容.
现在,最终,这些错误是有道理的。任务buildNumber 知道如何处理命令行值,但packageBin 不知道。如何设置此任务或这些任务集以允许传入版本号?
我见过this question,但答案链接到一个 sbt 插件,它似乎比我希望它做的事情多 100 件,包括我需要找到一种明确禁用的方法。我只希望版本号能够传入并在工件中使用。
编辑/更新:我通过切换回 Maven 解决了这个问题。
【问题讨论】: