maven assembly plugin example with a tutorial in maven
Maven is a project build management tool that automates the build phase of any java project.
whenever the project is built, it creates single modules with the format of jar or war.
so it builds zip your project includes configuration files and other XML files into different customized output module types like zip, jar, war, and other tar.gz formats.
As you know the maven-war plugin generates only war files, but in the projects, you need to archive them in different formats like zipped formats, in that case, we will use this plugin.
usually, any project builds either war or jar files. so if the project(artifact name is CloudHadoop), the result of your install command is CloudHadoop-1.0.0-SNAPSHOT.war. Moreover, it generates a single war file.
But using this plugin, we can multiple zip files like the jar, and zip with one execution.
if you want to generate a zip file (CloudHadoop.zip)of your project source code with its dependencies as well as configuration files, There are three tasks involved to use 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 is plugin-based architecture so you need to configure the plugin if we want to support any extra functionality.
Add the following plugin code in 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 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 result for the format of your zipped file.
FileSets defined the files in the source directory of your project. and the target directory is zipDirectory after your package goal is run.
Building your project with an assembler
assemble plugin can be run in two ways.
one way with an independent way of execution.
mvn assembly: assembly
```.once the above goal is executed, it creates a zipped file that contains files in the form of the same project directory structure.
and other ways are to attach the plugin execution with any predefined goals.
```cmd
maven package
How to create multiple jar files with maven
listing down high-level steps to generate multiple jar files within the single execution
- Define `maven-assembler-plugin configuration in pom.xml
- Define multiple executions element in the
maven-assembler-plugin
plugin configuration and configure multiple rules in custom assembler XML files - For each jar, define your custom assembler file with format type and source and target directories information and also include and exclude elements
- define the goal execution as single or attached 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.