19

When building a web application SBT is able to collect all my jar dependencies into the WAR file.

Is this possible to have SBT put all the jars I depend on in my non-web application into a directory so I can easily put them onto my class path when running the app?

Moritz
  • 14,144
  • 2
  • 56
  • 55
Brownie
  • 7,688
  • 5
  • 27
  • 39

3 Answers3

12

Yes, you can put something like this in your project definition class:

val libraryJarPath = outputPath / "lib"

def collectJarsTask = {
  val jars = mainDependencies.libraries +++ mainDependencies.scalaJars
  FileUtilities.copyFlat(jars.get, libraryJarPath, log)
}

lazy val collectJars = task { collectJarsTask; None } dependsOn(compile)

and run the task via collect-jars in your SBT console. This will copy the scala-library.jar and the jars used for compilation in a directory called lib in the same directory as your classes directory.

Moritz
  • 14,144
  • 2
  • 56
  • 55
  • Thanks Moritz that got me on the right track. It didn't include all my ivy dependencies though. – Brownie Apr 09 '11 at 00:52
  • 3
    The actual version that worked for me was: def collectJarsTask = { val jars = publicClasspath.get.filter(x=> !x.isDirectory) FileUtilities.copyFlat(jars, libraryJarPath, log) } – Brownie Apr 09 '11 at 00:55
  • I am new to using Build.scala. Please provide more specifics on where to place the above code. Specifically is it inside the BuildSettings or (somehow I do not know how..) in the Project object? – WestCoastProjects May 28 '14 at 01:50
3

In my honest opinion don't bother with sbt-assembly. I'm new with scala but I'm quite technology agnostic, I handle a lot of technologies and sbt-assembly it's not clean. Just an opinion.

I would recommend you sbt-pack. Awesome piece of work. It will give you the runnable scripts as well, for both.. WINDOWS AND LINUX.

https://github.com/xerial/sbt-pack

leonfs
  • 710
  • 1
  • 5
  • 14
2

You can use sbt-assembly to make a fat jar with all dependencies: https://github.com/sbt/sbt-assembly

  • I believe that sbt-assembly is great to creat fat jar, but not to collect dependencies. OP was asking for a tool to collect dependency jars. – Ameba Spugnosa Sep 08 '16 at 13:14