Creating a stand-alone jar using SBT

I was a heavy Maven user, and now I am gradually using SBT for some of my projects.

I would like to know how to use SBT to create a standalone Java project? This project must be packaged as a JAR file, and this JAR file will be used as a dependency in another SBT project.

In Maven, I can tell in my pom.xml what type of artifact it should produce when I create it. Is there something similar in SBT?

+44
sbt uberjar
Jan 05 '14 at 19:36
source share
5 answers

There is a difference between standalone and using a project as a dependency or another project. In the first case, you should use a plugin, for example sbt-assembly . It will create a single jar file containing the project class files, as well as all its dependencies. If you write an application, then you get a bi-directional jar that you can execute from anywhere.

If you want to use your project A as a dependency for another project B, you have different options. You can simply pack A class files using sbt package (@Channing Walton answer). You can then delete the resulting .jar file in the lib directory of project B. However, if A also requires a library, you must make sure that they also end up in the libraries of project B.

The best approach is to publish your project. You can only do this on your local machine using sbt publish-local . This will store the jar created by package in a special local directory that can be accessed from sbt in another project, along with a POM file that contains A. dependencies. It will use the group identifier (group) and artifact -ID (name) and version of your project A. For example, in build.sbt :

 name := "projecta" version := "0.1.0-SNAPSHOT" organization := "com.github.myname" scalaVersion := "2.10.3" publishMavenStyle := true 

After publishing with sbt publish-local you can add the following B dependency to your project:

 libraryDependencies += "com.github.myname" %% "projecta" % "0.1.0-SNAPSHOT" 

If you have a pure Java project, you can omit the suffix of the Scala version, i.e. in Project A:

 crossPaths := false autoScalaLibrary := false 

And then in Project B:

 libraryDependencies += "com.github.myname" % "projecta" % "0.1.0-SNAPSHOT" 

(using only one % character between group id and artifact).

Read more about publishing in sbt documentation .

+67
Jan 05 '14 at 20:57
source share

'sbt package' will create a jar file.

If you want it to run, you need to add the following to your .sbt configuration:

 mainClass in Compile := Some("your.main.Class") 
+30
Jan 05 '14 at 20:36
source share

Of course, you can use the "sbt package" command, it creates a jar file, but this jar will be without any dependencies. To run it, you must specify the "classpath" arg for the libraries.

In your case, you need a standalone executable file. And you need to add dependencies. You can use the 'assembly' plugin for SBT for this, see https://github.com/sbt/sbt-assembly/

After that, you can simply run the 'sbt assembly' command, it will provide a jar file with all the dependencies that you can deploy and run anywhere and anytime.

See the article for more details.

+7
Jan 04 '15 at 19:00
source share
 publishLocal 

Creates an artifact and publishes it in the local Ivy repository, making it available for your local project dependencies.

 publishM2 

the same as above, but the artifact is published in the local Maven repository instead of Ivy repo.

+4
Jan 05
source share

I think that the easiest way to create a separate jar with your project in it, sadly does not lie inside sbt.

I personally use my IDE: Intellij to make a jar (via the "build artifact" function).

Thanks to Intellij, I can easily choose which library I want to include in the jar or not (for example, scala stl).

IMHO, this is by far the easiest way to get an executable jar for your project.

If you put scala stl, you can run your jar using the java -jar command, if not, you need to run it somewhere with the correct version of scala installed using scala. "

+2
Jul 15 '14 at 13:52
source share



All Articles