我想通过custom configuration 提出一种方法。
以下是一个示例项目的build.sbt:
lazy val CustomPackage = config("custompackage") extend(Compile) describedAs("Custom package configuration")
packageBin in CustomPackage := {
val log = streams.value.log
import log._
info("""Returning custom packaging artifact, i.e. file(".")""")
file(".")
}
lazy val root = project in file(".") overrideConfigs (CustomPackage)
在上述构建配置中,custompackage 配置范围设置和任务。作为它如何改变packageBin 任务行为的一个例子,在这个范围中有一个(重新)定义。最后一行将自定义配置添加到项目中。
当您运行 SBT shell (sbt) 时,您会注意到真正的 packageBin 任务在 custompackage 范围内发生变化时没有受到影响。默认情况下,您处于Compile 配置中。要更改它,您需要使用扩展语法在另一个配置中执行任务。
[root]> configuration
[info] compile
[root]> show packageBin
[info] /Users/jacek/sandbox/so/config-package/target/scala-2.10/root_2.10-0.1-SNAPSHOT.jar
[success] Total time: 0 s, completed Feb 8, 2014 2:27:10 PM
当您将配置轴更改为 custompackage 时,任务 packageBin 会发生变化。
[root]> custompackage:configuration
[info] custompackage
[root]> show custompackage:packageBin
[info] Returning custom packaging artifact, i.e. file(".")
[info] .
[success] Total time: 0 s, completed Feb 8, 2014 2:27:20 PM
[root]> custompackage:packageBin
[info] Returning custom packaging artifact, i.e. file(".")
[success] Total time: 0 s, completed Feb 8, 2014 2:27:25 PM
全部在 SBT 0.13.1 下测试。
[root]> about
[info] This is sbt 0.13.1
[info] The current project is {file:/Users/jacek/sandbox/so/config-package/}root 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.3
[info] Available Plugins: com.typesafe.sbt.SbtGit, com.typesafe.sbt.SbtProguard, growl.GrowlingTests, org.sbtidea.SbtIdeaPlugin, np.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.3