2

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.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
josephpconley
  • 1,703
  • 12
  • 12
  • Do you want to create some task that depends on the package task or you want to do stuff before package gets run? – johanandren Feb 07 '14 at 16:09
  • I would like to clone the package task and modify the clone so as to not affect the original task. I want this clone of the package task to only package up a subset of my project (e.g. the models package). I was hoping there'd be a straightforward way to leverage the SBT API to do so. – josephpconley Feb 07 '14 at 16:38
  • Was a solution found for this problem? I am trying to do the same but could not find a way to achieve it – Miquel Jul 17 '14 at 11:14

1 Answers1

2

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
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
  • I believe your approach is correct in suggesting a custom scope. However, implementing is proving to be difficult. I believe the implementation found [here](http://sandarenu.blogspot.com/2012/04/exclude-resource-files-from-jar-file.html) is what I'm looking for. However, I'm unable to implement this in a custom scope. My latest attempt to do so can be found [here](https://gist.github.com/josephpconley/8916138). Any ideas what I did wrong in my implementation? – josephpconley Feb 10 '14 at 13:47
  • 1
    fwiw, I was going to suggest a custom config as well. (kudos to Jacek for taking the time to write it out and try it.) Joseph, it's good you showed code, but you don't explain why it doesn't work. Is there an error message? How is the actual behavior different from the expected behavior? – Seth Tisue Feb 14 '14 at 00:44
  • When trying the code in my gist and running custompackage:package, it packages up the entire project as if I were simply running package. However, when I modify my code to be "mappings in (Compile, packageBin)" and run package, it packages up only the models folder as expected. So for some reason, my initial "mappings in (CustomPackage, packageBin)" call isn't working. FWIW, I tried Jacek's code, but as I am using Play 2.1.1 I have to use sbt 0.12.2, and the example provided did not work as expected (target files were generated in the wrong directory and not filtered) – josephpconley Feb 14 '14 at 10:51