I’m starting to work with Maven but am not yet successfully thinking in Maven’s terms. I have a specific requirement and the docs aren’t giving me enough clues, so I could use a bit of help:
I’d like to create an assembly that
-
builds a
jar-with-dependencieslike the “standard” target of this name, but excluding a couple of the resources. I wantlog4j.propertiesand a couple of other configuration files to not be in the jar. -
builds a
.ZIPfile containing in its root directory the.jarfrom step 1 as well as the above mentioned config files.
I want to fire this assembly up from the command line (only), hence no need to tie to a phase (or goal? mojo?). Preferrably using either assembly:assembly or assembly:single.
- Do I need a custom assembly descriptor for this?
- And is it true I can’t nest it in the
pom.xml? So it goes insrc/assembly/something.xmland gets referenced with adescriptorRef? - Can I code this as two relatively simple assemblies, of which one builds on the other (i.e. the .Zip assembly uses the .Jar assembly) or do I have to do everything in one assembly?
Welcome on board, Carl! π
Just to clarify: the build lifecycle itself is made of
phases(compile, test, package, etc) and plugin goals (technically Mojos) are bound on phases. You then either invoke a phase… or just a specific plugin goal.Well, since you want behavior that the pre-defined descriptors don’t cover, yes. You’ll even need two of them (of for the uberjar, one for the zip).
Yes, that’s true (descriptors use a custom format) and they usually go into
src/main/assembly. And no,descriptorRefis for the built-in descriptors, you’ll have to usedescriptorhere.As hinted, you’ll need two assembly descriptors. Let me help a bit…
Let’s assume you have the following project structure:
$ tree . . βββ pom.xml βββ src βββ main βΒ Β βββ assembly βΒ Β βΒ Β βββ jar.xml βΒ Β βΒ Β βββ zip.xml βΒ Β βββ java βΒ Β βΒ Β βββ com βΒ Β βΒ Β βββ stackoverflow βΒ Β βΒ Β βββ App.java βΒ Β βββ resources βΒ Β βββ log4j.properties βββ test βββ java βββ com βββ stackoverflow βββ AppTest.javaWhere the
pom.xmlcontains the following configuration for the assembly plugin:The descriptor for the “filtered” uberjar (jar.xml) looks like this:
What this descriptor does is (in short):
And the descriptor for the zip (zip.xml) looks like this:
Which is (somehow) self explaining π
<directory>) at the root of the assembly<directory>) at the root of the assemblyFinally, just run
mvn assembly:assembly(that’s the goal intended to be used on the CLI).These are coming from the libraries that are unpacked. You can exclude them using
unpackOptions. Here is a modified version of thejar.xml: