37
votes

I'm new to . I want it to put all the dependency jar files as well as my jar file into one place. SBT will run the app, but I've got various dependencies scattered around and an .ivy folder full of things my jar file depends on indirectly.

So Is there a simple command to copy them all into a single place so I can distribute it to another machine?

7
Yes, this is real pain. Maybe I missed something but I really don't understand why this is not a part of Simple Build Tool - Alex Povar
@AlexPovar: I guess there are so many ways of doing this that picking one and standardizing it could be detrimental (but convenient I agree). - Erik Kaplun

7 Answers

19
votes

There are many plugins you can use: sbt-assembly, sbt-proguard, sbt-onejar, xitrum-package etc.

See the list of SBT plugins.

14
votes

Add the following line to your build.sbt file.

retrieveManaged := true

This will gather the dependencies locally

11
votes

Create a task in your build file like this:

lazy val copyDependencies = TaskKey[Unit]("pack")

def copyDepTask = copyDependencies <<= (update, crossTarget, scalaVersion) map {
  (updateReport, out, scalaVer) =>
    updateReport.allFiles foreach {
      srcPath =>
        val destPath = out / "lib" / srcPath.getName
        IO.copyFile(srcPath, destPath, preserveLastModified = true)
    }
}

Add the Task to a Project like this:

lazy val HubSensors =
  Project("HubSensors", file("HubSensors"), settings = shared ++ Seq(
    copyDepTask,
    resolvers ++= Seq(novusRels),
    libraryDependencies ++= Seq(
      jodatime
    )
  )) dependsOn(HubCameraVision, JamServiceProxy, HubDAL)

In the SBT console type:

project [Project Name]
pack
8
votes

Try sbt-pack plugin https://github.com/xerial/sbt-pack, which collects all dependent jars in target/pack folder and also generates launch scripts.

4
votes

You could also try SBT Native Packager: https://github.com/sbt/sbt-native-packager (sbt 0.7+)

This is still a WIP but will be used in Play Framework 2.2 in the coming weeks. With this, you can create standalone ZIP files, Debian packages (DEB), Windows installation packages (MSI), DMG, RPM, and so on.

2
votes

The SBT docs have a list of "One Jar Plugins":

0
votes