Sbt Packaging For Dummies

First of all I need some kind of disclaimer. Yes, I’ve started learning Scala and Akka Actors model, so that sometimes you will find the posts about scala here.

After I wrote my first hello world in Scala I asked myself how to get distributable package from the sources using SBT.

How to create a jar from my project?

Just type sbt package in cli inside your project folder. This command will create a single jar file, containing all your code.

Okay, how to get my dependencies too? You have two options: create fat-jar or create a package.

Create fat jar

Fat jar is very big jar-file containing compiled source code and all its dependencies in one file. Quite useful for one-time deployment, experiments and so on.

Add the following string into

# project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.12.0")

And configuration merging rules:

# build.sbt
mergeStrategy in assembly <<= (mergeStrategy in assembly) {
  (old) => {
    // discard META-INF
    case PathList("META-INF", xs @ _*) => MergeStrategy.discard
    // concat application.conf 
    case "application.conf"            => MergeStrategy.concat 
    case "reference.conf"              => MergeStrategy.concat
    case x => MergeStrategy.first
  }
}

Type sbt assembly in your project. You will get a very big jar file inside target folder.

Create a package

If you want something more distributable you can use sbt pack plugin. It will create a bunch of files ready for release.

Add the following string into

# project/plugins.sbt
addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.7.9")

To use the plugin with default options just add the following string into build.sbt:

# build.sbt
packAutoSettings

Type sbt pack in your project. You will get a folder called pack inside your target folder. It will contain Makefile, binaries to start an app and all jars.

Further reading

Alex Panshin avatar
About Alex Panshin
Software Engineer from Russia. Interested in PHP, Scala, microservices and Big/Fast Data stuff.
comments powered by HyperComments