Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Maven Assembly Plugin
3.
Usage and Goals
4.
pom.xml file
5.
Advantages of Assembly Plugin
6.
Disadvantages of Assembly Plugin
7.
Frequently Asked Questions
7.1.
Why use an Assembly Plugin?
7.2.
What is an uber jar file?
7.3.
What is the Maven Central Repository?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Maven Assembly Plugin

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

Introduction

Hey Ninja!!🥷Plugins are pre-written codes that we can use in our project on the fly. These plugins make it easy to manage the build and deployment of our project. 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. Learning how to handle these plugins and customise them according to our needs will immensely improve the quality of our project.

Maven Assembly Plugin

In this blog, we will learn about the maven assembly plugin. We will learn how to use it along with the plugin's goals. We will also go through a sample pom file to understand the working of this plugin. 

Maven Assembly Plugin

The maven assembly plugin allows developers to wrap the output of a project into a single archived distributable file that contains the dependencies, site documentation, modules and other files. It has prefabricated assembly descriptors that can quickly build assemblies. These descriptors handle operations such as packaging project artifacts into a single zip archive. We can even have our descriptors for better control over the project assembly.
 

Currently, the assembly plugin can give the output in the following formats:

 

⭐ zip

⭐ jar

⭐ dir

⭐ war

⭐ tar

⭐ tar.gz

⭐ tar.bz2

⭐ tar.xz

⭐ tar.snappy

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

Usage and Goals

Using the maven assembly plugin is relatively easy. Let's go through the steps required to use the plugin in our project.
 

✅ Step 1: Finalise the assembly descriptors to use.

✅ Step 2: Configure the pom.xml file according to the need.

✅ Step 3: Run the “mvn assembly:single”.
 

The main goal of the maven assembly plugin is the single goal. It is used to create all the assemblies.

pom.xml file

The most important file when using a plugin is the pom file. Let's look at the pom file having the maven assembly plugin.

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.ninja</groupId>
   <artifactId>ninja-assembly-plugin</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <scope>test</scope>
       </dependency>
   </dependencies>
   <build>
       <plugins>
           <plugin>
               <artifactId>maven-assembly-plugin</artifactId>
               <configuration>
                   <descriptorRefs>
                       <descriptorRef>jar-with-dependencies</descriptorRef>
                   </descriptorRefs>
                   <archive>
                       <manifest>
                           <mainClass>com.ninja.AssemblyApplication</mainClass>
                       </manifest>
                   </archive>
               </configuration>
               <executions>
                   <execution>
                       <id>make-assembly</id>
                       <phase>package</phase>
                       <goals>
                           <goal>single</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
       </plugins>
   </build>
</project>

Using the above pom file, we can use the assembly plugin in our project.

Advantages of Assembly Plugin

There are a lot of advantages to using the assembly plugin. Let's discuss a few of them:

✅ It is easy to build jar, war, and zip files.

✅ It is easy to convert our project into a different environment using the assembly plugin.

✅ After conversion into a different environment, we don't have to handle dependencies such as injection, builds and processes.

✅ Adding other dependencies is a breeze.

✅ Running the project is easy after the build process.

✅ Adding the plugin in the pom file will automatically fetch all the dependencies.

Disadvantages of Assembly Plugin

There aren't many disadvantages, but still, we can consider some facts as disadvantages. Let's see a few of them:

✅ We must install maven to use the assembly plugin.

✅ To create uber jar files, we have to use a more advanced plugin such as the maven shade plugin.

✅ We have to edit the pom.xml file and insert the plugin code.

✅ To run the application, we have to build the assembly plugin.

 

Must Read Apache Server

Frequently Asked Questions

Why use an Assembly Plugin?

It makes it easy to create a single distributable file containing all the files and dependencies. Custom descriptors give more control over the build and distribution of the project.

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 the Maven Central Repository?

It is a library containing all the essential and standard plugins required while working with maven.

Conclusion

This blog has shed light on Maven Assembly Plugin. It is a handy automation tool that is very needed for web and java developers. We also went through a sample pom file and the advantages and disadvantages of the assembly plugin. We also saw the usage and goals of the plugin.

Check out other related articles to learn more:


And many more on our platform Coding Ninjas Studio.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass