Search code examples
javamavenjarmaven-assembly-plugin

Package maven app into a single jar - with an exception


I have some dependencies in my app, and I want all of the dependencies to be packaged into a single jar during the build - with the exception of a single dependency (a utilities jar of the team). Found out how to build to a single jar using maven-assembly-plugin, don't know how to exclude a specific dependency.

This is the relevant pom.xml file:

<repositories>
    <repository>
        <id>local-maven-repo</id>
        <url>file://repository_path</url>
    </repository>
</repositories>
....
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.10</source>
                <target>1.10</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <finalName>app-name</finalName>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.company.appname.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>
    </plugins>
</build>
...
<dependencies>
    <dependency>
        <groupId>com.company.utils</groupId>
        <artifactId>utils</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

Solution

  • Probably Including and Excluding Artifacts will help you.

    To be more specific, you need to create your own assembly.xml file:

    <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>jar-with-dependencies-exclude</id>
        <formats>
            <format>jar</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
            <dependencySet>
                <outputDirectory>/</outputDirectory>
                <useProjectArtifact>true</useProjectArtifact>
                <unpack>true</unpack>
                <scope>runtime</scope>
                <excludes>
                    <exclude>com.fasterxml.jackson.datatype:jackson-datatype-jsr310</exclude>
                </excludes>
            </dependencySet>
        </dependencySets>
    </assembly>
    

    And then change your plugin configuration to use this file:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <finalName>app-name</finalName>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.company.appname.Main</mainClass>
                </manifest>
            </archive>
            <descriptors>
                <descriptor>src/main/assembly.xml</descriptor>
            </descriptors>
            <appendAssemblyId>false</appendAssemblyId>
        </configuration>
    </plugin>