In this example, we will learn to use Maven Shade plugin to package a java project along with its dependencies into a fat jar or uber jar.
Maven Shade Plugin Syntax
Let’s go through basic syntax of maven shade plugin before learning how to use it in your project.
<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>
<goal>shade</goal>
tells that it should be run in package phase.ManifestResourceTransformer
creates the entries inMANIFEST.MF
file as key-value pairs in the<manifestEntries>
.- You can use more available transfers as per your need.
Sample Maven Project
Let’s create a sample maven project and add some dependencies into 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>
Project structure looks like this.

Run Maven Package Goal
> mvn package
When you run the package
goal in 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
.
- MavenShadeExample-uber.jar : This is fat/uber jar with all dependencies inside it.
- dependency-reduced-pom.xml : This generated maven file is your pom.xml file minus all dependencies.
- original-MavenShadeExample-uber.jar : This jar has been generated by executing
dependency-reduced-pom.xml
.
Verify all classes in generated by running below command.
jar -tvf MavenShadeExample-uber.jar
Output will be listed in command prompt with all classes inside it.
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
Drop me your questions in comments section.
Happy Learning !!
senthil
I am getting an error while executing the package goal
“A required class was missing while executing org.apache.maven.plugins:maven-shade-plugin:2.4.3:shade: org/codehaus/plexus/util/xml/XmlStreamWriter”