Search code examples
mavenmaven-assembly-plugin

Seperate the jars using maven assembly plugin


I need to separate the jars into different folders.

Inside my project, I have several modules, i.e: module1, module2, and assembly.

By using maven assembly plugin, I would like to put generated jar from modules1 and module2 into target/modules and all the dependencies into target/dependencies.

How can I achieve this requirement?

Thanks


Solution

  • Based on the fact that your project is structured like :

    project
        |- module1
        |- module2
        |- assembly
            |- pom.xml
            |- src
                |- assembly
                    |- bin.xml
    

    assembly should depend on module1 and module2; set these dependencies in assembly/pom.xml :

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>module1</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>module2</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
    

    You must also add maven-assembly-plugin in assembly/pom.xml :

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
            <descriptors>
                <descriptor>src/assembly/bin.xml</descriptor>
            </descriptors>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    assembly:single is bound on package phase, to create the assembly when running mvn package.

    Finally, define assembly/src/assembly/bin.xml as follows :

    <assembly   xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
        <id>bin</id>
        <formats>
            <format>dir</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
            <dependencySet>
                <outputDirectory>modules</outputDirectory>
                <includes>
                    <include>${project.groupId}:*:*</include>
                </includes>
                <excludes>
                    <exclude>${project.groupId}:${project.artifactId}:*</exclude>
                </excludes>
            </dependencySet>
            <dependencySet>
                <useTransitiveDependencies>true</useTransitiveDependencies>
                <outputDirectory>dependencies</outputDirectory>
                <excludes>
                    <exclude>${project.groupId}:*:*</exclude>
                </excludes>
            </dependencySet>
        </dependencySets>
    </assembly>
    
    • format defines the format you want for the assembly (here a directory, but could be tar.gz, zip, ...)
    • first dependencySet defines a folder modules/ where all artifacts from the same groupId will be put. Here you can also control if you want only some of the artifacts (for example if you want only module1). assembly JAR is excluded from this folder, as this module is only used to create the assembly
    • second dependencySet defines a folder dependencies/ where all dependencies (and transitive dependencies) will be put. Dependency here means artifact with a different groupId, as states the excludes clause

    mvn package will then generate the assembly (in assembly/target/ folder), named assembly-${project.version}-bin, with the structure you want.