Maven assembly plugin example with a tutorial
Maven is a project build management tool that automates the build phase of a Java project. When the project is built, it creates single modules in the form of JAR or WAR files. It can package your project, including configuration files and other XML files, into various customized output module types such as ZIP, JAR, WAR, and other formats like tar.gz.
As you know, the Maven War Plugin generates only WAR files. However, in projects, there may be a need to archive them in different formats, such as zipped formats. In such cases, we use this plugin
Typically, a project builds either war
or jar
files. For instance, if the project has the artifact name ‘CloudHadoop
, the result of your install command might be CloudHadoop-1.0.0-SNAPSHOT.war
generating a single war file.
However, by using this plugin, you can create multiple ZIP files, such as jar and zip, with a single execution.
If you want to generate a ZIP file (‘CloudHadoop.zip’) containing your project’s source code, dependencies, and configuration files, there are three tasks involved in using the maven-assembly-plugin in our project.
- Configure the maven-assembly-plugin plugin
- Create a custom assembly file
- Run assembly goal using maven commands
How to Configure the maven-assembly-plugin plugin
Maven follows a plugin-based architecture, so you need to configure the plugin to support any additional functionality.
Add the following plugin code to your pom.xml
<project>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.2.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>project</descriptorRef>
</descriptorRefs>
<descriptors>
<descriptor>src/assembly/zipCode.xml</descriptor>
</descriptors>
<finalName>${pom.artifactId}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</project>
maven-assembly-plugin
is a plugin defined with a plugin
tag with groupId and version descriptorRef
represents the type of folder structure in the zipped bundled file.
the possible elements for descriptorRef
are jar-with-dependencies
(jar format like MANIFEST), project
(zipped in project directory structure), src
(only the src folder with subfolders)
descriptor
specifies the customized assembly XML that contains the code.
The execution
phase represents the goal where this assembly code is executed.
How to create a custom assembly descriptor file?
We can define our custom assembly file to create our module, and the file name is zipCode.xml
. You can define multiple assembly files by configuring these files in the plugin declaration.
<assembly>
<id>src</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main</directory>
<outputDirectory>zipDirectory</outputDirectory>
<includes>
<include>\*.\*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
The format
specifies the format of the resulting zipped file. FileSets
defines the files in the source directory of your project, and the target
directory is the zipDirectory
after your package goal is run.
Building your project with an assembler
The assembly plugin can be executed in two ways, one of which is independently using the below command.
mvn assembly: assembly
Once the above goal is executed, it creates a zipped file containing files in the same project directory structure.
Another way is to attach the plugin execution with any predefined goals. For example package goal.
maven package
How to create multiple jar files with Maven
To generate multiple JAR files within a single execution, follow these high-level steps:
- Define the
maven-assembly-plugin
configuration in the pom.xml. - Within the
maven-assembly-plugin
configuration, define multiple<executions>
elements and configure multiple rules in custom assembler XML files. - For each JAR, create a custom assembler file specifying the format type, source, and target directories information, and include/exclude elements.
- Define the goal execution as either a single or attached goal with any predefined goals. Finally, build your project using the Maven command in the command line.
Conclusion
you understand the basic idea of the Maven assembler plugin with a basic example.