How to create custom “package” task to jar up only

2019-04-12 14:27发布

I have a Play project running on Play 2.1.1.

I would like to define a separate and distinct task to jar up only a specific package in my Play project (e.g. just the models package). I do not wish to override the current package tasks as I want to preserve their behaviour.

How would I go about creating a custom task based off of the current package task?

I've looked at Custom Settings and Tasks in the SBT documentation but these examples are fairly trivial and don't give any examples that use the SBT library.

1条回答
男人必须洒脱
2楼-- · 2019-04-12 15:08

I'd like to propose an approach with custom configuration.

The following is a build.sbt of a sample project:

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)

In the above build configuration, there's custompackage configuration that scopes settings and tasks. As an example of how it can change the behaviour of packageBin task there's a (re)definition in this scope. The last line adds the custom configuration to the project.

When you run SBT shell (sbt), you will notice that the real packageBin task is untouched while it changes in custompackage scope. By default, you're in Compile configuration. To change it, you need to use the extended syntax to execute a task in another configuration.

[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

When you change configuration axis to custompackage the task packageBin changes.

[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

All tested under 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
查看更多
登录 后发表回答