EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Maven Shade Plugin

By Payal UdhaniPayal Udhani

Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML
Home Software Development Software Development Tutorials Maven Tutorial Maven Shade Plugin

Maven Shade Plugin

Introduction to Maven Shade Plugin

There are many plugins available for maven projects. The main aim of the plugins is to provide the capability to form the package of the artifacts in the maven project and its dependencies into an uber or fat jar and rename certain packages. Uber is a german word that stands for above which means it is one level up, unlike the other normal jars. Uber also called a fat jar is sufficient for independently running the project and contains all the dependencies and required jars withing itself for a particular project along with its project files. Shade plugin of maven provides us with the capability to create such an uber jar. There are also other plugins available such as assembly plugin. But shade plugin is the most preferred one as it provides relocating facility that helps in avoiding the class name conflicts having the same name in the classpath. Shade plugin has only one goal that is it can be only used in the package phase and the main purpose lies in creating the shaded jar that can be independently executed.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,130 ratings)

How we can use Shade Plugin to Build the Jar?

Let us understand how we can use shade plugin to build the jar using an example in the eclipse IDE.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Step 1: The first step is to create a new maven project in your eclipse IDE that contains the general format of the maven project shown below. Select create a simple project(skip archetype selection) option that is sufficient for most of the java applications even those that use spring framework in it.

maven shade plugin1

Step 2: we are creating a project with a group is as com.educba and artifact-id as shade-plugin-demo with the following specifications as shown in the image.

maven shade plugin2

Step 3: After successfully creating your maven project, you will see the following project structure having J2SE 1.5 used in JRE system libraries.

maven shade plugin3

Step 4: Now, open the pom.xml file. It will look like this:

maven shade plugin4

Step 5: Change the Java version to 1.8. and add the plugins tag and insert the maven compiler plugin in it as shown and save the file. Update the Maven project by right-clicking on your project -> Maven -> Update option to see the reflection. You will see that the java version now used by your JRE system libraries is 1.8.

maven shade plugin5

Step 6: Add a new class file in the src/main/java/com/shadedemo path named MainDemo.java. This file will contain your main method. Mention the name of the package as it is a good practice of not using the default package name.

maven shade plugin6

Step 7: Note that till now, the maven dependencies directory is empty. Add dependencies in your pom.xml file that your project might need such as we are adding google guava and JUnit for my usage. You can add the dependencies by making use of the central maven repository where you will get the dependency tag that you need to add in pom for your dependency after searching for it. Here’s the link of the central repository – https://mvnrepository.com/repos/central and search link is –https://search.maven.org/. After addition check maven dependencies.

maven shade plugin7

Step 8: Add some code in your main method. we will add a string named sample name and initialize it. Using the guava method isNullOrEmpty we will check whether it is not null or empty and then add a console to print the message as shown below. After running it as the java application you will see the message on the console saying – Yeah! A string is valid and its value is: Payal

Application

Step 9: Now, our application is ready. Let us check where the jars of our project will get the store. They are stored inside the folder named target. Open the target folder where your jars will be seen. In my case, the path is as follows:

Target Folder

Step 10: Add the shade plugin tag from the https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html link into your pom.xml file and mention the fully qualified name of your main file in the main class tag such as in my case com.shadedemo.MainDemo as follows:

Shade demo

Step 11: Right-click on your maven project and run as a maven build and mention goals that are usual ones such as clean and install to build the jar file for your project using shade plugin.

jar file

Step 12: After clicking on the run button the console will show all the build process notifications and will tell the build is done successfully if everything works out to be well. The path where the jar file will be located is also displayed on the console. In our case its as follows:

[INFO] Installing /home/a/java_oops_session/educba/shade-plugin-demo/pom.xml to /home/a/.m2/repository/com/educate/shade-plugin-demo/0.0.1-SNAPSHOT/shade-plugin-demo-0.0.1-SNAPSHOT.pom

Build process

Step 13: After the build is successful, open your target folder where your jar is created and open a terminal at that location. To run your main file execute the following command on your terminal:

java -jar nameOfJar.jar

In our case, we can see a new jar file named shade-plugin-demo-0.0.1-SNAPSHOT.jar been created in my target folder and executing the command –

java -jar shade-plugin-demo-0.0.1-SNAPSHOT.jar

gives the following output:

maven shade plugin13

You can change the name of your final jar that is built by using a shade plugin by adding a tag inside the build tag with name final name and mentioning the name of the target file to be created inside that tag.

Here, is how our pom.xml file looks like:

<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.educba</groupId>
<artifactId>shade-plugin-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass> com.shadedemo.MainDemo</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

main file MainDemo.java contains the following content in it:

package com.shadedemo;
import com.google.common.base.Strings;
public class MainDemo {
public static void main(String[] args) {
String sampleName = "Payal";
if(!Strings.isNullOrEmpty(sampleName)){
System.out.println("Yeah! String is valid and its value is :- "+sampleName);
}
}
}

Conclusion

We can use the shade plugin in maven to obtain the single jar file also called as uber/fat jar file that is self-sufficient and can be run independently as it contains all the dependencies and project code inside it. This can be done by simply adding the plugin tag with shade plugin related information as shown in the above example.

Recommended Articles

This is a guide to Maven Shade Plugin. Here we discuss an introduction to Maven Shade Plugin, how we can use it to build the Jar it with Code and output. You can also go through our other related articles to learn more –

  1. What is Maven
  2. Maven Life Cycle
  3. Maven POM File
  4. Maven Plugins
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Maven Training (4 Courses, 6 Projects) Learn More