Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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.
⭐ Step 3: Specify the Group ID and Artifact ID as shown in the image below.
⭐ 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.
⭐ Step 5: Open the pom.xml file. It should look something like this.
⭐ 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.
⭐ 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.
⭐ 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.
⭐ 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.
⭐ Step 10: Our application is now ready. It's stored in the target directory. See the image below for more clarification.
⭐ 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.
⭐ 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.
⭐ Step 13: After clicking on the run button, the build process will show the logs in the console.
⭐ 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.
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.
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!
Live masterclass
Become a YouTube Analyst: Use Python to analyze viewers data
by Coding Ninjas
04 Feb, 2025
02:30 PM
Get hired as an Amazon SDE : Resume building tips
by Coding Ninjas
03 Feb, 2025
02:30 PM
Expert tips: Ace Leadership roles in Fortune 500 companies
by Coding Ninjas
03 Feb, 2025
12:30 PM
Become a YouTube Analyst: Use Python to analyze viewers data