Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Dependencies in JAR using SBT package

Tags:

scala

sbt

Apparently project dependencies are not being packaged into the jar generated by:

sbt package

How can dependencies be included?

like image 504
BAR Avatar asked Sep 30 '15 06:09

BAR


People also ask

Which is the correct way to add dependencies in sbt file?

If you have JAR files (unmanaged dependencies) that you want to use in your project, simply copy them to the lib folder in the root directory of your SBT project, and SBT will find them automatically.

How do I run a JAR file in sbt?

Solution. Create a directory layout to match what SBT expects, then run sbt compile to compile your project, sbt run to run your project, and sbt package to package your project as a JAR file. Unlike Java, in Scala, the file's package name doesn't have to match the directory name.

What does sbt package do?

If you run sbt package , SBT will build a thin JAR file that only includes your project files. The thin JAR file will not include the uJson files. If you run sbt assembly , SBT will build a fat JAR file that includes both your project files and the uJson files.


2 Answers

Well, I use sbt-assembly plugin to create jar with dependencies,

(1) add sbt-assembly to projects/assembly.sbt

echo 'addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.8")' > project/assembly.sbt

(2) run sbt clean assembly to build the jar. It will create ${name}-assembly-${version}.jar in target/scala-${scalaVersion}

(3) Only in case you get infamous de-duplicate error, use assemblyMergeStrategy as described in here

like image 119
prayagupa Avatar answered Sep 28 '22 10:09

prayagupa


There's a project called onejar that will package a project and all its dependencies into a single jar file. There is an SBT plugin as well:

https://github.com/sbt/sbt-onejar

However if you're just looking to create a standard package (deb, rpm, etc.) there is sbt-native-packager:

https://github.com/sbt/sbt-native-packager

It can place all your dependencies into a Linux package and add the appropriate wrappers to load all your dependencies and start your program or service.

like image 28
djsumdog Avatar answered Sep 28 '22 10:09

djsumdog