Apache Maven - Assembly

Updated: 2019-01-03

If the following descriptor is set, the final jar will be <ProjectName>-<Version>-jar-with-dependencies.jar

<descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>

Full example:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

To set the final name:

<configuration>
    <finalName>FinalName</finalName>
    ...
</configuration>

To remove the suffix:

<configuration>
    <appendAssemblyId>false</appendAssemblyId>
    ...
</configuration>

To exclude some of the dependency jars from assembly, add <scope>provided</scope> to dependency section

<dependency>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <scope>provided</scope>
</dependency>

To copy dependent jars to /lib folder in the package: (format [groupId]:[artifactId]:[version])

<assembly>
...
<dependencySets>
    <dependencySet>
        <outputDirectory>/lib</outputDirectory>
        <includes>
            <include>*:commons-collections:*</include>
            <include>*:commons-compress:*</include>
            <include>*:commons-io:*</include>
            <include>*:commons-jexl:*</include>
            <include>*:commons-lang:*</include>
            <include>*:commons-logging:*</include>
            ...
        </includes>
    </dependencySet>
</dependencySets>
</assembly>

Definition of jar-with-dependencies

<assembly>
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

The key is <unpack>true</unpack>. If it is false, the dependent jars will be packaged directly into the outer jar, somehow they are not accessible; if true, all dependent jars will be unpacked and files will be exposed to classpath.

Exclude

To include/exclude specific dependencies, make a copy of this descriptor, and add <includes> or <excludes> inside <dependencySets>

<assembly>
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <excludes>
                <exclude>*:some-lib*</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>
<excludes>
</excludes>

Multiple Excution

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>

            <executions>
                <execution>
                    <id>make-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>your.main.class</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <finalName>FinalName</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                    </configuration>
                </execution>

                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>