Publishing fat jar created by sbt-assembly

On a project at work I use sbt-assembly to generate an executable fat jar, which I want to publish to my company’s Nexus repo in addition to the regular ones. That turns out to be rather straight forward to set up in sbt. Firstly I set the assembly jar name to be the normal artifact name with an “assembly” classifier (the naming should be a good choice for both ivy and maven):

jarName in assembly <<= (name, scalaVersion, version) { _ + "_" + _ + "-" + _ + "-assembly.jar" }

Then I add to my Build.scala:

addArtifact(Artifact("my-project", "assembly"), sbtassembly.Plugin.AssemblyKeys.assembly)

where I declare the Artifact with the "assembly" classifier and tell sbt it is the file generated by the assembly task (provided by sbt-assembly). addArtifact method returns a SettingsDefinition, which is a Seq of settings that should be added to the existing settings of the project. Putting everything together, the full project config looks like this:

lazy val proj = Project("my-project", file("."), 
  settings = Defaults.defaultSettings ++
    sbtassembly.Plugin.assemblySettings ++
    addArtifact(Artifact("my-project", "assembly"), sbtassembly.Plugin.AssemblyKeys.assembly) ++
    Seq(
      name := "my-project",
      // libraryDependencies and whatnot
      jarName in assembly <<= (name, scalaVersion, version) { _ + "_" + _ + "-" + _ + "-assembly.jar" }
    )
)

That’s it. Now sbt publish/publish-local will also publish the fat jar.

Leave a comment