How to create custom “package” task to jar up only specific package in SBT?

时光怂恿深爱的人放手 提交于 2019-12-21 23:25:08

问题


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:


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


来源:https://stackoverflow.com/questions/21615592/how-to-create-custom-package-task-to-jar-up-only-specific-package-in-sbt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!