I am coding in scala and using SBT with sbt-assembly plugin. My project has a lot of external libraries, and therefore sbt-assembly takes a long time to pack the JAR file. I would like to have a separate jar files for my code and for the dependencies. Therefore, I could simply recompile and repack only my code. How to do this?
Progress so far:
The following commands in the build.sbt
prevents sbt-assembly from packing the libraries:
assembleArtifact in packageScala := false
assembleArtifact in packageDependency := false
Then separate jars can be built by passing following commands: assembly
, assembly-package-scala
and assembly-package-dependency
. They make three jars: one containing my program, one with scala libraries and one with all the dependencies.
However, the jar file is not executable anymore because it does not see the dependencies in the separate jar file. I think I need to add a classpath to assembly-sbt, but I am not sure how to do this.
See Setting Classpath Still Can't Find External Jar. Oracle says:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
So, you have to say:
java -cp "foo-assembly-0.1-deps.jar:foo-assembly-0.1.jar" Main
where Main
is the name of the main class, and :
is the file separator on your OS (;
on Windows) .
Currently there seems to be a bug in assembly-package-scala
and it comes out empty, but it shouldn't matter because assembly-package-dependency
contains Scala library too.
Use sbt-pack plugin at https://github.com/xerial/sbt-pack . So far I've found it the cleanest way to pack the project and dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With