Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Medium

Maven Shade Plugin

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Maven is a build automation tool mainly used for Java projects. It is managed by Apache Software Foundation and can execute projects in different languages, including C#, Scala, Ruby etc. To support maven, we have several plugins available. These plugins are used to create jar files, war files, unit testing, code compilation etc. Each plugin generally provides a set of goals and is defined in the 'pom.xml' file.

Maven Shade Plugin


In this blog, we will discuss one such plugin called the maven shade plugin. We will go through a sample project examining the working and capabilities of this plugin.

Maven Shade Plugin

The maven shade plugin is a versatile plugin that is used for packaging the artifacts in uber-jar, including all of its dependencies. It also provides the feature to rename the packages of some of its dependencies. We can create a single independent jar file containing everything necessary to run the project. Other plugins, such as the assembly plugin, are also available, but the maven shade plugin is more advanced than the maven assembly plugin. The maven shade plugin has one defined goal:

✨ shade: It is invoked during the package phase.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Sample Project

Let's discuss a sample project where we will learn how to build a jar file using the shade plugin. 

Step 1: Install Eclipse IDE from the official website.

Step 2: Create a new maven project and select the simple project.

New Maven Project

 

Step 3: Specify the Group ID and Artifact ID as shown in the image below.

Config


Step 4: Click on 'finish' to create the project. You will see the project structure similar to the image below. It will have J2SE 1.5 in the JRE System Libraries.

Project Structure

 

Step 5: Open the pom.xml file. It should look something like this.

pom file

 

Step 6: Insert the plugin tag and maven compiler plugin as shown in the image. Update the project by pressing Alt+F5 to see the changes.

Inserting plugin tag


Step 7: Add a new class file in the src/main/java/com/ninja directory. Name the class as ‘MainDemo.java’. This file will contain the main method.

java class

 

Step 8: Our dependency directory is empty. Let's add a few dependencies in the pom.xml file. To add the dependency, search it in the central maven repository after searching for the dependency. We will use guava and JUnit in our sample project.

Central Repository

 

Step 9: Add some content in the main method to verify our dependency. We are using the isNullOrEmpty method from the guava dependency to check this fact. Run the project to see the output in the console.

Main demo java

 

Step 10: Our application is now ready. It's stored in the target directory. See the image below for more clarification.

target folder


Step 11: It's time to add the maven shade plugin. You can search for it from the central maven repository. Add all the content in the pom.xml file. Make sure to edit the package name according to your package.

Adding maven shade plugin


Step 12: Right-click on the project, run it as maven build, and set the goals to build the jar files using the shade plugin.

Setting up goals

 

Step 13: After clicking on the run button, the build process will show the logs in the console.

build


Step 14: Now, the build process is done. From the target directory, open a terminal and run the following command to execute the main file.

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


Final pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ninja</groupId>
    <artifactId>ninja-shade-plugin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.9</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven.compiler.plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <target>${java.version}</target>
                    <source>${java.version}</source>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <groupId>org.apache.maven.plugins</groupId>
                <version>3.2.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <transformers>
                                <transformer />
                                <transformer implementation=" org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.ninja.MainDemo</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


Final MainDemo.java file:

//demo java file
package com.ninja;


import com.google.common.base.Strings;


public class MainDemo {
    public static void main(String[] args) {
        String Name = "Manish";
        if (!Strings.isNullOrEmpty(Name)) {
            System.out.println("The string is correct:" + Name);
        }
    }
}

 

Must Read Apache Server

Frequently Asked Questions

What is Maven?

Maven is a build automation tool created by Apache Software Foundation. It can handle C#, Scala, Ruby etc. languages.

What is an uber jar file?

It is one level up from a simple jar file. It contains all of our packages and dependencies in a single jar file. It is independent and can be executed without any other needs.

What is a package?

A package is a complete set of files needed in a project. It contains all the dependencies and source code and can be shipped as the final product.

Conclusion

This blog has shed light on Maven Shade Plugin. It is a handy automation tool that is very needed for web and java developers. We also went through a sample project explaining how to build a jar file using the shade plugin.

More blogs on Maven:


You can find more Maven blogs on our platform: Coding Ninjas Studio.

To excel in the technical field, visit our platform Coding Ninjas Studio to learn more about JavaScript, DSA, Competitive Programming, System Design, etc. Enroll in our courses and refer to the mock test and problems available. To prepare for placements, follow our interview experiences and interview bundle curated especially for cracking technical jobs.

Happy Coding!

Topics covered
1.
Introduction
2.
Maven Shade Plugin
3.
Sample Project
4.
Frequently Asked Questions
4.1.
What is Maven?
4.2.
What is an uber jar file?
4.3.
What is a package?
5.
Conclusion