15

I need to build a single jar, including dependencies, for one of my sub-projects so that it can be used as a javaagent.

I have a multi-module sbt project and this particular module is the lowest level one (it's also pure Java).

Can I (e.g. with sbt-onejar, sbt-proguard or sbt assembly) override how the lowest level module is packaged?

It looks like these tools are really designed to be a post-publish step, but I really need a (replacement or additional) published artefact to include the dependencies (but only for this one module).

UPDATE: Publishing for sbt-assembly are instructions for a single project, and doesn't easily translate into multi-project.

samthebest
  • 30,803
  • 25
  • 102
  • 142
fommil
  • 5,757
  • 8
  • 41
  • 81

1 Answers1

20

Publishing for sbt-assembly are instructions for a single project, and doesn't easily translate into multi-project.

People have been publishing fat JAR using sbt-assembly & sbt-release without issues. Here's a blog article from 2011: Publishing fat jar created by sbt-assembly. It boils down to adding addArtifact(Artifact(projectName, "assembly"), sbtassembly.AssemblyKeys.assembly) to your build.sbt (note that the blog is a little out of date AssemblyKeys is now a member of sbtassembly directly).

For sbt 0.13 and above, I prefer to use build.sbt for multi-projects too, so I'd write it like:

import AssemblyKeys._

lazy val commonSettings = Seq(
  version := "0.1-SNAPSHOT",
  organization := "com.example",
  scalaVersion := "2.10.1"
)

val app = (project in file("app")).
  settings(commonSettings: _*).
  settings(assemblySettings: _*).
  settings(
    artifact in (Compile, assembly) ~= { art =>
      art.copy(`classifier` = Some("assembly"))
    }
  ).
  settings(addArtifact(artifact in (Compile, assembly), assembly).settings: _*)

See Defining custom artifacts:

addArtifact returns a sequence of settings (wrapped in a SettingsDefinition). In a full build configuration, usage looks like:

...
lazy val proj = Project(...)
  .settings( addArtifact(...).settings : _* )
...
samthebest
  • 30,803
  • 25
  • 102
  • 142
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • thanks, but I get a compile error if I do that in the `build.scala` – fommil Apr 15 '14 at 09:48
  • 2
    “a compile error” — what compile error? can't offer help if we can see neither your code nor the error message – Seth Tisue Apr 16 '14 at 02:21
  • 1
    thanks Eugene, your edits fixed it (NOTE: we discussed this elsewhere and it turned out to be a typo in the docs). – fommil Apr 19 '14 at 22:37
  • Mine seems to be creating the assembly, but then publishing the packaged jar, but not the assembly jar. Do you know what the issue might be? – nickn Mar 15 '16 at 14:01
  • @nickn it sounds like you're not setting a classifier for the assembled jar, so it defaults to the normal artficat name and then conflicts with the non-assembled jar being made/published first – EdgeCaseBerg Oct 25 '16 at 15:59