sbt-assembly: how to link dependencies jar with main jar?
Asked Answered
M

2

12

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.

Mcdonough answered 25/7, 2013 at 19:41 Comment(0)
P
4

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.

Pedagogy answered 26/7, 2013 at 17:31 Comment(0)
D
6

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.

Dendrology answered 25/7, 2013 at 22:0 Comment(3)
It works! and it is fast: time is reduced from about 40sec to few sec. Technical note: I needed to include scala-library-2.10.2-assembly.jar as well because otherwise the scala repl was unstable. I could make compile it with sbt 0.12.4 and sbt-assembly 0.9.0.Mcdonough
Is there any way to package this class reference into the main jar file? So it would be possible to execute jar by clicking on it.Mcdonough
sbt-onejar can make a jar that contains other jars and it packages faster. But I hear sbt-assembly starts up faster because presumably sbt-onejar'ed jar needs to unzip on the fly. sbt-assembly 0.9.0 caches lib unzips so you shouldn't have to split them up.Dendrology
P
4

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.

Pedagogy answered 26/7, 2013 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.