A typical project requirement is to aggregate the project output along with its dependencies, modules, and other files into a single distributable archive. An assembly is a group of files, directories, and dependencies that are assembled into an archive format and distributed. Maven provides prefabricated assembly descriptors to build these assemblies. The descriptors handle common operations, such as packaging a project's artifact, along with the dependencies.
Maven should be set up on your system and verified to work. To do this, refer to Chapter 1, Getting Started.
project-with-assembly
.<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.3</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.packt.cookbook.App</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
mvn clean package
[INFO] --- maven-assembly-plugin:2.5.3:single (make-assembly) @ project-with-assembly --- [INFO] Building jar: C:projectsapache-maven-cookbookproject-with-assembly argetproject-with-assembly-1.0-SNAPSHOT-jar-with-dependencies.jar
C:projectsapache-maven-cookbookproject-with-assembly arget>java -jar project-with-assembly-1.0-SNAPSHOT-jar-with-dependencies.jar 07:13:25.660 [main] INFO com.packt.cookbook.App - Hello World
We made the following changes to the pom file:
jar-with-dependencies
, one of the prefabricated assembly descriptors provided by the Maven Assembly plugin. This creates a single JAR with all the dependencies of the project.archive
configuration to specify the main class of the project. This is to make the JAR file executable.package
phase.When Maven ran, it used the preceding configurations to assemble a JAR with dependencies in the package phase. We could run this as a normal executable JAR.
Besides predefined descriptors, the Maven Assembly plugin also allows us to create custom descriptors that can have fine-grained control over the contents of the assembly.
The Assembly plugin can also build an assembly from a multi-module project, where the modules can be part of the final assembly.
While opening the JAR file, you would have observed that all the dependant JARs have been unpacked as well.
This is due to the default configuration for the predefined descriptor. Let us see how to create the same distribution but retain dependant JARs as they are. To do this, we will now use one Maven JAR plugin, which uses a custom class loader to load dependant JARs within the parent JAR:
project-with-one-jar
).<plugin> <groupId>org.dstovall</groupId> <artifactId>onejar-maven-plugin</artifactId> <version>1.4.4</version> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>one-jar</goal> </goals> </execution> </executions> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.packt.cookbook.App</mainClass> </manifest> </archive> </configuration> </plugin>
<pluginRepositories> <pluginRepository> <id>onejar-maven-plugin.googlecode.com</id> <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url> </pluginRepository> </pluginRepositories>
mvn package
java -jar project-with-one-jar-1.0-SNAPSHOT.one-jar.jar 06:57:45.995 [main] INFO com.packt.cookbook.App - Hello World
We can see that in contrast to the assembly JAR, the executable JAR is created without unpacking the libraries (dependencies) involved.
lib
folder in the JAR:The dependant JARs are stored in the lib
folder.
18.118.198.81