Search code examples
scalasbtsbt-assembly

How to force adding provided dependency to fat jar using sbt-assembly plugin?


I have a Spark project which I usually package with sbt-assembly. All spark dependencies are marked provided and not included in my fat jar. I want to have another command to build a really fat jar with all dependencies, spark included. I am trying the following with no luck:

lazy val standalone = project
.dependsOn(mainProj % "compile->compile;test->test;provided->compile")
.settings(
  logLevel in assembly := Level.Debug,
  assemblyOption in assembly := (assemblyOption in assembly).value.copy(includeScala = true, includeDependency = true)
)

Note, the answers to How to add "provided" dependencies back to run/test tasks' classpath? explain how to add provided dependencies to the runtime classpath, however my question is about how to have them end up in the packaged artifact after executing sbt assembly.


Solution

  • To build a truly fat jar which packages everything including provided dependencies, we could redefine fullClasspath in assembly like so

    assembly / fullClasspath := (Compile / fullClasspath).value
    

    If we put this in a separate command like so

    commands += Command.command("assemblyTrulyFatJar") { state =>
      """set assembly / fullClasspath := (Compile / fullClasspath).value""" :: "assembly" :: state
    }
    

    then executing sbt assemblyTrulyFatJar should package everything, while sbt assembly keeps its default behaviour.