Creating Executable Fat Jar with Maven Shade Plugin

In this Maven example, we will learn to use the Maven Shade plugin to package a java application with its dependencies into a fat jar or uber jar. For simple usecases, we can also consider using the Maven assembly plugin.

1. Maven Shade Plugin Syntax

Let’s go through the basic syntax of the maven shade plugin before learning how to use it in our project.

  • <goal>shade</goal> tells that it should be run in package phase.
  • ManifestResourceTransformer creates the entries in MANIFEST.MF file as key-value pairs in the <manifestEntries>.
  • You can use more available transfers as per your need.
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.4.3</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
          <goal>shade</goal>
      </goals>
      <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <manifestEntries>
                  <Main-Class>com.howtodoinjava.demo.App</Main-Class>
                  <Build-Number>1.0</Build-Number>
                </manifestEntries>
            </transformer>
          </transformers>
      </configuration>
    </execution>
  </executions>
</plugin>

2. Demo

Let’s create a simple maven project and add some dependencies to it. This is the pom.xml file for it.

<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.howtodoinjava.demo</groupId>
  <artifactId>MavenShadeExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>MavenShadeExample</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.4</version>
    </dependency>
  </dependencies>
 
  <build>
    <finalName>MavenShadeExample-uber</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>com.howtodoinjava.demo.App</Main-Class>
                    <Build-Number>1.0</Build-Number>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

The project structure looks like this.

Maven Shade Plugin Structure
Maven Shade Plugin Structure

3. Package Application as Executable Jar

Package the application with ‘mvn clean package‘ command.

$ mvn clean package

When you run the package goal in the project’s root directory, you will get two generated jar files i.e. and one extra pom.xml file named with dependency-reduced-pom.xml.

  1. MavenShadeExample-uber.jar : This is fat/uber jar with all dependencies inside it.
  2. dependency-reduced-pom.xml : This generated maven file is your pom.xml file minus all dependencies.
  3. original-MavenShadeExample-uber.jar : This jar has been generated by executing dependency-reduced-pom.xml.

Verify all classes generated by running the below command. The command output will list all classes/files in the jar.

$ jar -tvf MavenShadeExample-uber.jar

Manifest file content could also be verified.

Manifest-Version: 1.0
Build-Number: 1.0
Build-Jdk: 1.6.0_33
Created-By: Apache Maven
Main-Class: com.howtodoinjava.demo.App

Happy Learning !!

Leave a Reply

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial