How to package a Scala project to a .jar with sbt?

How to package a Scala project to a .jar file with sbt? Manually packaging the .class files are fine but too tedious.

A jar of just the project classes

The command:

sbt package

will produces the main artifact as a jar into target/scala-2.x.y/project_name_2.x.y-zz.jar.

Continuously building the package:

sbt ~package

Standalone jar with all dependencies

If you want to build a standalone executable jar with dependencies, you may use the sbt-assembly plugin. And then build it by

sbt assembly

The standalone will be in target/project_name-assembly-x.y.jar.

You can run it by

java -jar project_name-assembly-x.y.jar [class.with.main.function]

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *