Apache Maven Assembly Plugin

The Apache Maven Assembly Plugin is a powerful tool that allows you to create distributions of your project in a variety of formats, including ZIP, TAR, and JAR. This plugin is especially useful for bundling your project with its dependencies, configuration files, and other necessary resources. In this guide, we will explore the essential features of the Maven Assembly Plugin and provide practical examples to help you get started.

Introduction to Maven Assembly Plugin

The Maven Assembly Plugin enables you to create an assembly (a distribution) from your project's output. Assemblies can be used to distribute your project as a single archive file, making it easier to share and deploy.

Latest Version

As of this writing, the latest version of the Maven Assembly Plugin is 3.3.0. Using the latest version ensures access to the newest features and improvements.

Setting Up the Maven Assembly Plugin

To use the Maven Assembly Plugin, you need to configure it in your project's pom.xml file. Let's go through the steps to set up a Maven project with the Assembly Plugin.

Step 1: Create a Maven Project

Run the following command to create a new Maven project:

mvn archetype:generate -DgroupId=com.example -DartifactId=assembly-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command will generate a simple Maven project with the following structure:

assembly-plugin-demo
|-- src
|   |-- main
|   |   `-- java
|   |       `-- com
|   |           `-- example
|   |               `-- App.java
|   `-- test
|       `-- java
|           `-- com
|               `-- example
|                   `-- AppTest.java
|-- pom.xml
`-- target

Step 2: Add Maven Assembly Plugin Configuration

Navigate to the project directory and open the pom.xml file. Add the Maven Assembly Plugin configuration inside the <build> section:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>assembly-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.example.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Explanation

  • <groupId>: Specifies the group ID for the Maven Assembly Plugin.
  • <artifactId>: Specifies the Maven Assembly Plugin.
  • <version>: The version of the plugin you are using. Ensure you use the latest version.
  • <descriptorRefs>: Specifies the type of assembly to create. In this case, jar-with-dependencies is used to create a JAR file with all dependencies included.
  • <archive>: Configures the archive settings, such as the main class for the JAR manifest.
  • <executions>: Defines the execution of the plugin, specifying when to create the assembly. In this example, the assembly is created during the package phase.

Step 3: Create Main Class

Open src/main/java/com/example/App.java and update the App class to print "Hello, World!":

package com.example;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Step 4: Build the Project

To build the project and create the assembly, use the following command:

mvn clean package

After the build completes, you will find the assembled JAR file in the target directory:

target/
|-- assembly-plugin-demo-1.0-SNAPSHOT.jar
|-- assembly-plugin-demo-1.0-SNAPSHOT-jar-with-dependencies.jar
|-- classes
|-- generated-sources
|-- maven-archiver
|-- maven-status
|-- surefire-reports
|-- test-classes
`-- ...

The assembly-plugin-demo-1.0-SNAPSHOT-jar-with-dependencies.jar file contains your project's classes and all its dependencies.

Step 5: Run the Packaged JAR

To run the assembled JAR file, use the following command:

java -jar target/assembly-plugin-demo-1.0-SNAPSHOT-jar-with-dependencies.jar

This command will output:

Hello, World!

Conclusion

The Maven Assembly Plugin is used to create distributions for your project. By integrating it into your Maven build process, you can easily package your project with all its dependencies and other necessary resources. This guide provided a complete overview of setting up and using the Maven Assembly Plugin, along with practical examples to help you get started. With this knowledge, you can efficiently create and distribute your Java applications.

Comments