Rolintocour
Rolintocour

Reputation: 3148

SBT: Custom command/task to publish FAT ou SLIM jar

I use the sbt assembly plugin. However I need to publish both slim and fat jar, i.a. publish the usual jar and the assembly jar.

I noticed hat, to publish a fat jar, I need to:

artifact in (Compile, assembly) := {
    val art = (artifact in (Compile, assembly)).value
    art.withClassifier(Some("assembly"))
}

addArtifact(artifact in (Compile, assembly), assembly)

However I need to be able to distinguish between fat and slim jar.

I came to the following code, which doesn't do what I want (it publishes a slim jar):

def helloSbt = Command.command("publishFatJat") { state =>
println("Publishing fat jar")

artifact in (Compile, assembly) := {
    val art = (artifact in (Compile, assembly)).value
    art.withClassifier(Some("assembly"))
}

addArtifact(artifact in (Compile, assembly), assembly)

Command.process("publish", state)
state
                                            }
commands += helloSbt

publishFatJat publishes the jar, but the slim one.

WHat's wrong with my code? Do you have any idea how to do it? Thx.

Upvotes: 0

Views: 313

Answers (1)

Evan S.
Evan S.

Reputation: 91

These settings:

artifact in (Compile, assembly) := {
  val art = (artifact in (Compile, assembly)).value
  art.withClassifier(Some("assembly"))
}
addArtifact(artifact in (Compile, assembly), assembly)

Should be in your project definition .settings() or in the root of your build.sbt, and not under the command definition.

Since you have that code, I assume you already know why publishing fat JARs is not recommended. Just in case you don't, please read this. As the documentation mentions below, you should really have a second project specifically for publishing, without dependencies, if you want to do this.

Upvotes: 1

Related Questions