pgraham
pgraham

Reputation: 446

SBT task to extract the jar created by package-src somewhere

I would like to create a task that depends on another task with a jar file as output (e.g. package-src), which then extracts the resulting jar somewhere?

Note: I'm not interested in the library/method used to perform the extraction, only how I would define a task that invokes such a library or method.

Upvotes: 2

Views: 941

Answers (1)

user500592
user500592

Reputation:

The relevant documentation pages are Tasks and TaskInputs. For unzipping, you can use sbt.IO.unzip(...).

First, we need to define the task's key (in a .scala build definition). The task is going to return the set of unzipped files.

val unzipPackage = TaskKey[Set[File]]("unzip-package", "unzips the JAR generated by package-src")

Then, we add a setting like this one:

unzipPackage <<= (packageSrc, target in unzipPackage, streams) map { (zipFile, dir, out) =>
  IO createDirectory dir  
  val unzippedFiles = IO unzip (zipFile, dir, AllPassFilter)
  out.log.info("Unzipped %d files of %s to %s" format (unzippedFiles size, zipFile, dir))
  unzippedFiles
}

This let's us define the output directory as a setting, too:

target in unzipPackage <<= target / "unzippedPackage"

Hope this helps.

Upvotes: 3

Related Questions