Building a fat jar using Maven

Maven provides other plugins using which we can generate the jar with all its dependencies which is usually called Maven FatJar, which is used as an executable Jar file. FatJar 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.

These Fat Jars are mostly useful to build executable jar file, for suppose, if we are developing a microservice, if we don't create fat jar file, we need to all its dependencies in one folder, add this folder to the classpath, and start the microservice, for each time we need to do these steps in manually, but if we generate the fat jar as it includes all its dependencies, not needed to add to the classpath, we can directly run the command to start the microservices.

Table of Content :

Fat JARs are also useful if you are packaging 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.

There are different way to generate the Fat JHar file using maven.

1). Using Maven Assembly Plugin

You need to add this plugin in your pom.xml as given below.

This plugin also supports, if some of the packages are not using in the application, we don’t need in the final fat jar, we can exclude those package in the exclude section, which will improve the class loading performance.

<plugin > <groupId >org.apache.maven.plugins </groupId > <artifactId >maven-assembly-plugin </artifactId > <version >3.2.0 </version > <configuration > <descriptorRefs > <descriptorRef >jar-with-dependencies </descriptorRef > </descriptorRefs > <archive > <manifest > <mainClass >org.sample.Application </mainClass > </manifest > </archive > </configuration > <executions > <execution > <id >make-assembly </id > <phase >package </phase > <goals > <goal >single </goal > </goals > </execution > </executions > </plugin >

2). Using Maven-Shade Plugin

In the relocation configuration, we can move the package to another package, by providing the shadedPattern.

It supports, if some of the packages are not using in the application, we don't need in the final fat jar, we can exclude those package in the exclude section, which will improve the class loading performance.

For suppose, if a jar file contains 5 packages, but we are using only one package, then instead of excluding 9 packages, include 1 package.

<plugin > <groupId >org.apache.maven.plugins </groupId > <artifactId >maven-shade-plugin </artifactId > <version >3.2.4 </version > <executions > <execution > <phase >package </phase > <goals > <goal >shade </goal > </goals > <configuration > <relocations > <relocation > <pattern >org.sample.plexus.util </pattern > <shadedPattern >org.shaded.sample.plexus.util </shadedPattern > <excludes > <exclude >org.sample.plexus.util.xml.pull.* </exclude > </excludes > </relocation > </relocations > </configuration > </execution > </executions > </plugin >

3). Maven Command to Build Fat JAR

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

mvn clean package

After configuring the shade plugin in your build the command mvn package will create one single jar with all dependencies merged into it.

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

4). Using onejar-maven-plugin

You can use the onejar-maven-plugin for packaging. Basically, it assembles your project and its dependencies in as one jar, including not just your project jar file, but also all external dependencies as a "jar of jars", e.g.

<build > <plugins > <plugin > <groupId >com.jolira </groupId > <artifactId >onejar-maven-plugin </artifactId > <version >1.4.4 </version > <executions > <execution > <goals > <goal >one-jar </goal > </goals > </execution > </executions > </plugin > </plugins > </build >

5). Using maven shade plugin

An alternative is to use the maven shade plugin to build an uber-jar.

<plugin > <groupId >org.apache.maven.plugins </groupId > <artifactId >maven-shade-plugin </artifactId > <version > Your Version Here </version > <configuration > <!-- put your configurations here -- > </configuration > <executions > <execution > <phase >package </phase > <goals > <goal >shade </goal > </goals > </execution > </executions > </plugin >

6). Spring boot application

For spring boot application use just following plugin.

<plugin > <groupId >org.springframework.boot </groupId > <artifactId >spring-boot-maven-plugin </artifactId > <configuration > <fork >true </fork > <mainClass >${start-class} </mainClass > </configuration > <executions > <execution > <goals > <goal >repackage </goal > </goals > </execution > </executions > </plugin >

In this article, we have seen  How to Build a fat jar using Maven.

In this article we have seen differnt ways to Build a fat jar using Maven.