0

Because of the large size of some resource files, I'd like sbt package to create 2 jar files at the same time, e.g. project-0.0.1.jar for the classes and project-0.0.1-res.jar for the resources.

Is this doable?

[SOLUTION] based on the answer below thanks to @gilad-hoch

1) unmanagedResources in Compile := Seq()

Now it's just classes in the default jar.

2)

val packageRes = taskKey[File]("Produces a jar containing only the resources folder")
packageRes := {
  val jarFile = new File("target/scala-2.10/" + name.value + "_" + "2.10" + "-" + version.value + "-res.jar")
  sbt.IO.jar(files2TupleRec("", file("src/main/resources")), jarFile, new java.util.jar.Manifest)
  jarFile
}

def files2TupleRec(pathPrefix: String, dir: File): Seq[Tuple2[File, String]] = {
  sbt.IO.listFiles(dir) flatMap {
    f => {
      if (f.isFile) Seq((f, s"${pathPrefix}${f.getName}"))
      else files2TupleRec(s"${pathPrefix}${f.getName}/", f)
    }
  }
}

(packageBin in Compile) <<= (packageBin in Compile) dependsOn (packageRes)

Now when I do "sbt package", both the default jar and a resource jar are produced at the same time.

Marc Grue
  • 5,865
  • 3
  • 16
  • 23

1 Answers1

0

to not include the resources in the main jar, you could simply add the following line:

unmanagedResources in Compile := Seq()

to add another jar, you could define a new task. it would generally be something like that: use sbt.IO jar method to create the jar. you could use something like:

def files2TupleRec(pathPrefix: String, dir: File): Seq[Tuple2[File,String]] = {
    sbt.IO.listFiles(dir) flatMap {
        f => {
            if(f.isFile) Seq((f,s"${pathPrefix}${f.getName}"))
            else files2TupleRec(s"${pathPrefix}${f.getName}/",f)
        }
    }
}
files2TupleRec("",file("path/to/resources/dir")) //usually src/main/resources

or use the built-in methods from Path to create the sources: Traversable[(File, String)] required by the jar method. that's basically the whole deal...

gilad hoch
  • 2,846
  • 2
  • 33
  • 57