How to determine execution order using maven-assembly-plugin

I am working on moving a project from Ant to Maven. The final distribution I need to deliver is a zip containing an executable jar with all its dependencies. Here is part of my pom:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-4</version> <configuration> <finalName>ProjectDistribution</finalName> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>fullQualifiedNameToMainClass</mainClass> <addClasspath>true</addClasspath> </manifest> </archive> <descriptors> <descriptor>${project.basedir}/src/main/assembly/dep.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>jar-with-dependencies</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> <execution> <id>dist</id> <phase>assembly</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> 

And here is the build file:

 <assembly> <id>dist</id> <formats> <format>zip</format> </formats> <!-- 1st approach--> <!--files> <file> <source> /target/ProjectDistribution.jar </source> <outputDirectory>/</outputDirectory> </file> </files--> <fileSets> <!-- 2nd approach--> <!--fileSet> <directory>/target</directory> <outputDirectory></outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet--> <fileSet> <directory>/HelpFiles</directory> <outputDirectory></outputDirectory> <includes> <include>*.*</include> </includes> </fileSet> </fileSets> 

I start compiling 1.mvn, 2.-mvn and build 3.-mvn: single

The problem I'm dealing with is that

It generates a jar with all the dependencies, and it generates a zip, but does not include the jar in zip. I really need to figure out a way to create an assembly, first create a jar and wait until it is created (because its size is 5 MB), and then create a zip. Right now, the 1st and 2nd approaches - from the build file - are commented out, however I used both, and none of them seem to work.

Any help would be greatly appreciated!

Eric

+4
source share
2 answers

To get this working, you need to separate the <configuration> and put it in two plugin executions:

 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-4</version> <executions> <execution> <id>jar-with-dependencies</id> <phase>verify</phase> <goals> <goal>single</goal> </goals> <configuration> <finalName>ProjectDistribution</finalName> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>fullQualifiedNameToMainClass</mainClass> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </execution> <execution> <id>dist</id> <phase>verify</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptors> <descriptor>${project.basedir}/src/main/assembly/dep.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> </plugins> </build> 

The first execution will create a jar file. In the second run, this JAR file and put it in a ZIP file along with other files. In this configuration, you can simply run mvn verify or mvn install to create the assembly.

Two more things to consider:

  • You must use the validation phase to build the assembly because the jar-with-dependencies descriptor includes the project artifact itself. At the packaging stage, the project artifact will not be ready for packaging.
  • The jar-with-dependencies descriptor has very limited capabilities for creating a JAR file with all the dependencies. You should use maven-shade-plugin .
+8
source

You mix the predefined jar-with-dependencies with a custom zip descriptor. Usually you need one of them - not both.

It looks like you need a zip that contains your project artifact along with its dependencies. You do not need to create jar-with-dependencies . If, however, you want to have one executable jar with all the dependencies in it, then it is not clear why you need it again.

+1
source

All Articles