Looking for an Expert Development Team? Take two weeks Trial! Try Now

How to build a FatJar using Maven?


Maven is one of the most used tools for Java application development.

Maven packaging will create a Jar file without dependencies.

Maven provides other plugins using which we can generate the jar with all its dependencies which is usually called FatJar, which is used as an executable Jar file.

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. The same will be applicable for docker images, as we can pass the single command to start the docker container.

Maven provides different ways to generate the Fat jar:

Maven Assembly Plugin:

Add the below Maven plugin in pom.xml file:

<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> <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>

Note: if the project has no main class then we can omit the archive section.

descriptorRef is the name that will be appended the package name.

The phase is used to specify when this maven goal should execute, suggest to specify the package because when we invoke mvn package, these jar with all dependencies will also be generated.

Maven has proved to be a useful tool required for development of a JAVA application.

Visit us and acquire the extensive knowledge of developing executable Jar files.

The drawback of this plugin is some class with the same package is added to the classpath, this assembly plugin will have only class.

Relocating classes feature is not supporting in the above plugin.

Maven provides another plugin shade-plugin will have this feature if the duplicate package names are present we configure with alternate package names.


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

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

This plugin also supports, if some of the packages are not using in the Application security solutions, 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 10 packages, but we are using only one package, then instead of excluding 9 packages, include 1 package.

We can use includes tag similar excludes.

Review fat jar:

Execute the below command to observe the fat jar folder structure:

jar tf target\magellan-connector-msexchange-online-graph-cmis-1.0.0-SNAPSHOT-jar-with-dependencies.jar

Third-party libraries are also available to prepare fat jar like one-jar maven plugin etc.

META-INF/ META-INF/MANIFEST.MF org/ org/apache/ org/apache/chemistry/ org/apache/chemistry/opencmis/ org/apache/chemistry/opencmis/server/ org/apache/chemistry/opencmis/server/support/ org/apache/chemistry/opencmis/server/support/filter/ org/apache/chemistry/opencmis/server/support/query/ META-INF/DEPENDENCIES META-INF/LICENSE META-INF/NOTICE org/apache/chemistry/opencmis/server/support/CmisServiceWrapper.class org/apache/chemistry/opencmis/server/support/filter/JsonPrettyPrinter.class org/apache/chemistry/opencmis/server/support/filter/LoggingFilter$LoggingInputStream.class org/apache/chemistry/opencmis/server/support/filter/LoggingFilter$LoggingOutputStream.class org/apache/chemistry/opencmis/server/support/filter/LoggingFilter$LoggingRequestWrapper.class org/apache/chemistry/opencmis/server/support/filter/LoggingFilter$LoggingResponseWrapper.class org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.class org/apache/chemistry/opencmis/server/support/query/AbstractPredicateWalker.class org/apache/chemistry/opencmis/server/support/query/CalendarHelper.class ..... com/opentext/ com/opentext/connectors/ com/opentext/connectors/msexchangeonlinegraph/ com/opentext/connectors/msexchangeonlinegraph/annotations/ com/opentext/connectors/msexchangeonlinegraph/cmis/ com/opentext/connectors/msexchangeonlinegraph/cmis/apiutils/ com/opentext/connectors/msexchangeonlinegraph/cmis/domain/ com/opentext/connectors/msexchangeonlinegraph/cmis/impl/ com/opentext/connectors/msexchangeonlinegraph/cmis/objects/ com/opentext/connectors/msexchangeonlinegraph/cmis/typedef/ com/opentext/connectors/msexchangeonlinegraph/cmis/utils/ com/opentext/connectors/msexchangeonlinegraph/annotations/ConnectorCmisService.class com/opentext/connectors/msexchangeonlinegraph/cmis/apiutils/CommonApi.class com/opentext/connectors/msexchangeonlinegraph/cmis/apiutils/ConversationContainer.class com/opentext/connectors/msexchangeonlinegraph/cmis/apiutils/FolderContainer.class com/opentext/connectors/msexchangeonlinegraph/cmis/apiutils/ICmisChildContainer.class com/opentext/connectors/msexchangeonlinegraph/cmis/apiutils/MessageContainer.class com/opentext/connectors/msexchangeonlinegraph/cmis/apiutils/RootContainer.class com/opentext/connectors/msexchangeonlinegraph/cmis/apiutils/UserContainer.class com/opentext/connectors/msexchangeonlinegraph/cmis/domain/Bearer.class com/opentext/connectors/msexchangeonlinegraph/cmis/domain/Body.class com/opentext/connectors/msexchangeonlinegraph/cmis/domain/DirectoryRole.class com/opentext/connectors/msexchangeonlinegraph/cmis/domain/DirectoryRoles.class ...


In the current content we learned what is the fat jar, its uses, how to create fat jar file in different ways.

Related article

Spring Cloud Config provides server and client modules for supporting externalized Configuration in a distributed system.

Microservices have become one of the enablers in the digital transformation journey as it is always focused and built on Business Need" and Single Responsibility. Microservices pattern enables scalability, speed, efficiency, and agility because of De-centralized API.

Layers of docker file just like, files are generated after running some command. Docker provides the different storage drivers for Linux flavored operating systems and windows.

DMCA Logo do not copy