JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Maven Shade Plugin – UberJar / Fat Jar Example

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>
                <transformerimplementation="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>
  1. <goal>shade</goal> tells that it should be run in package phase.
  2. ManifestResourceTransformer creates the entries in MANIFEST.MF file as key-value pairs in the <manifestEntries>.
  3. 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"
    <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.

Maven Shade Plugin Structure
Maven Shade Plugin Structure

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

.