如何创建自定义“包”任务以仅在SBT中查找特定包?

时间:2014-02-06 22:37:02

标签: playframework-2.0 sbt

我在 Play 2.1.1 上运行了一个Play项目。

我想定义一个单独且不同的任务,只能在我的Play项目中查找特定的包(例如只是模型包)。我不想覆盖当前的package任务,因为我想保留他们的行为。

如何根据当前package任务创建自定义任务?

我在SBT文档中查看了Custom Settings and Tasks,但这些示例相当简单,并没有给出任何使用SBT库的示例。

1 个答案:

答案 0 :(得分:2)

我想提出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
相关问题