maven: multi-module project assembly into single jar
Solution 1
I think you are looking for the Maven Shade Plugin:
http://maven.apache.org/plugins/maven-shade-plugin/index.html
Packages up any number of dependencies into an uber package depenency. This can then be deployed to a repository.
Solution 2
To package classes from all modules to a single jar I did the following:
Created additional module that is used only for packing contents of all other modules to a single jar. This is usually reffered to as a assembly module. Try calling this module same as target jar file.
In pom.xml of this new module i added maven-assemby-plugin. This plugin packages all classes and puts them in single file. It uses additional configuration file (step 4.)
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>go-framework-assemby</id> <phase>package</phase><!-- create assembly in package phase (invoke 'single' goal on assemby plugin)--> <goals> <goal>single</goal> </goals> <configuration> <descriptors> <descriptor>src/main/assemble/framework_bin.xml</descriptor> </descriptors> <finalName>framework</finalName> <appendAssemblyId>false</appendAssemblyId> </configuration> </execution> </executions> </plugin> </plugins> </build>
3.In pom.xml of this new module I also added dependencies to all other modules including parent pom. Only modules included in dependencies will be packed in target jar file.
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>fwk-bam</artifactId>
<version>${project.version}</version>
</dependency>...
4.Finally i created assembly descriptor in assembly module (file: src/main/assemble/framework_bin.xml)
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>all-jar</id>
<formats>
<format>jar</format> <!-- the result is a jar file -->
</formats>
<includeBaseDirectory>false</includeBaseDirectory> <!-- strip the module prefixes -->
<dependencySets>
<dependencySet>
<unpack>true</unpack> <!-- unpack , then repack the jars -->
<useTransitiveDependencies>false</useTransitiveDependencies> <!-- do not pull in any transitive dependencies -->
</dependencySet>
</dependencySets>
</assembly>
Jeroen
Updated on June 05, 2022Comments
-
Jeroen about 2 years
I have a multi-module project and want to create a single jar containing the classes of all my modules. Inside my parent POM, I declared the following plugin:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>bin</descriptorRef> </descriptorRefs> </configuration> </plugin>
However, when running mvn assembly:assembly, only the source from the parent folder (empty) are included. How do I include the sources from my modules into the archive?
-
Stijn Van Bael over 13 yearsAdding a child project depending on the other child projects (make sure they are all included) did the trick. Declare the shade:shade execution in the child project, configured with a single include for all artifacts under the multi-module group. See maven.apache.org/plugins/maven-shade-plugin/examples/… for details.