I am working on the migration of a project from Ant to Maven. The final distribution I need to deliver is a zip containing an executable jar with all its depencencies. 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 assembly 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 run 1.- mvn compile, 2.- mvn package, and 3.- mvn assembly:single
The problem I am dealing with is that
It does generate the jar with all the dependencies and it does generate the zip but it does not includ the jar in the zip. I pretty much need to figure out a way of making the assembly first generate the jar and wait until it is created (because its size is 5 MB) and then create the zip. Right now the 1st and 2nd approaches -from the assembly file- are commented out, however, I have used both and none of them seem to work.
Any help will be greatly appreciated!
Eric
Maven builds are executions of an ordered series of phases. These phases are determined by the lifecycle that is appropriate to your project based on its packaging. Therefore, you control when a plugin's goal is executed by binding it to a particular phase.
The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files. Your project can easily build distribution "assemblies" using one of the prefabricated assembly descriptors.
Defines the rules for matching and working with files in a given base directory.
This descriptor specifies the type of assembly archive to create, the contents of the assembly, and the ways in which dependencies or its modules are bundled with an assembly. <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
To get this working you need to split the <configuration>
and put it into the 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 the jar file. The second execution will take that JAR file and
put it into the ZIP file together with the other files. With this configuration, you can just execute mvn verify
or mvn install
to create the assembly.
There are two other things to consider:
You are mixing the pre-defined jar-with-dependencies
with a custom zip
descriptor. You would normally want one of them - not both.
It looks like you want a zip which contains your project artifact along with its dependencies. For this you would not need to create a jar-with-dependencies
. If, however, you do want a single executable jar with all the dependencies in it, then it is not clear why you need to zip it again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With