Packaging a Scala program to distribute is not a straight forward affair with the standard library. You will need to pull in your Scala and Java dependencies and have a script that adds these to your classpath before launching the program. There is fortunately an easier way…

Enter sbt-assembly, it is a packaging tool for making a single fat jar, which can then be distributed and run as simply as:

java -jar <your assembly jar>

The best thing is that more recent versions of the sbt-assembly plugin have made it incredibly easy to do. First, in your repo, create a file if doesn’t exist called project/plugins.sbt and add the following line.

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")

And that’s it! Now from the command line if you run sbt assembly it will run your tests and build the jar. The log output will even tell you the full path to the jar.

If you would like to skip the tests, you can run the following instead to create the jar:

sbt "set test in assembly := {}" assembly

There is also functionality for splitting the fat jar into two parts, one for your code and the other for your dependencies. This can be helpful if you have to upload your code to a server and you don’t want to have to transfer the whole fat jar each time. I will cover that in a future post, let me know if you are interested!