Tech and Media Labs
This site uses cookies to improve the user experience.


Build a Fat JAR With Maven

Jakob Jenkov
Last update: 2019-09-01

Maven can build a Fat JAR from your Java project. A Fat JAR is a single JAR file that contains all the compiled Java classes from your project, and all compiled Java classes from all JAR files your project depends on (see Maven Dependencies). In this Maven Fat JAR Tutorial I will show you how to create a Fat JAR with Maven.

Fat JARs are handy when you need to build an executable JAR file, e.g. a microservice executable. You can package your microservice and all of its dependencies into a single JAR file. This makes execution much easier, because you don't have to list all the JAR files your microservice depends on, on the classpath. That simplifies the command to startup the microservice.

Fat JARs are also handy if you need to package your application inside a Docker container. Instead of having to add each JAR file your application depends on to the Docker container, you only have to add the Fat JAR of our application. Again, this is much shorter, easier, and it makes your Dockerfile independent of what external dependencies (JAR files) your application uses.

Fat JAR POM Configuration

To get Maven to build a Fat JAR from your project you must include a Fat JAR build configuration in your project's POM file. You configure Maven to build a Fat JAR from your project by including the maven-assembly-plugin in your POM file's plugin section. Maven refers to an output product that it builds as an assembly. Hence the name maven-assembly-plugin. Here is an example snippet from a POM file showing an example of a maven-assembly-plugin configuration:

<build>
    <finalName>my-project-name</finalName>
    <plugins>

        <!-- other Maven plugins ... -->

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.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>

The configuration XML element inside the maven-assembly-plugin configuration contains the descriptorRef which tells Maven what type of assembly this configuration should build. The value jar-with-dependencies tells Maven to build a JAR file with dependencies which is another term for a Fat JAR.

The executions XML element tells Maven which Maven build phase and goal this Maven plugin should be executed during. The maven-assembly-plugin should always be executed during the package phase.

Maven Command to Build Fat JAR

The Maven command to make Maven build the Fat JAR for your project is:

mvn clean package

When you execute Maven package phase with the maven-assembly-plugin configuration shown earlier, Maven will output a Fat JAR in the target directory, into which Maven outputs all of its other build products (e.g. compiled classes, generated JavaDocs etc.). The Fat JAR will be named like this:

my-project-name-jar-with-dependencies.jar

The part my-project-name of the above Fat JAR file name comes from the finalName XML element included in the top of the build XML element in the example shown earlier in this tutorial.

You can read more about Maven commands in my Maven Commands tutorial.

You can read more about the Maven Assembly Plugin in the official Maven Assembly Plugin Documentation .

Full POM File With Fat JAR Configuration

Here is a full Maven POM file with a Fat JAR configuration, just to show you how such a POM file can look:

<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.jenkov.myprojectname</groupId>
    <artifactId>my-project-name</artifactId>
    <version>1.1.0</version>
    <packaging>jar</packaging>

    <name>My Project Name</name>

    <dependencies>
    
        <!-- Dependencies used by project -->

    </dependencies>

    <build>
        <finalName>my-project-name</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.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>
</project>    

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC