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!