Maven assembly & Shade plugins examples & uber jar creation

Maven assembly plugin is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distribution archive like zip, tar, tar.gz, war, etc. Another example where this is handy is to create deployable files for different environments like myapp-1.10-SNAPSHOT-dev.zip, myapp-1.10-SNAPSHOT-test.zip, and myapp-1.10-SNAPSHOT-prod.zip where each containing environment related properties files packaged.

The simplest way to use assembly plugin is to have the plugin configured in your pom.xml file and when you run “mvn package” to create “${project.artifactId}-batch.zip” file

But often, you want to create different zip files for different environments like ${project.artifactId}-${version}-${env}.zip, where the environment can be “DEV”, “TEST”, “PROD”, etc having different environment config files.

3 steps to create an assembly

  1. choose or write the assembly descriptor files to use like dev-assembly.xml, prod-assembly.xml, etc
  2. configure the Assembly Plugin in your project’s pom.xml, and include the above descriptor files
  3. run “mvn assembly:single” on your project.

Step 1: Create the assembly files for different environments

Now the dev-assembly.xml file

The prod environment prod-assembly.xml will have something like

Step 2: The pom.xml file with assembly plugin configured.

Step 3: Run the mvn command.

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

Now to generate different packages like myapp-1.10-SNAPSHOT-dev.zip, myapp-1.10-SNAPSHOT-test.zip, and myapp-1.10-SNAPSHOT-prod.zip

Creating an Uber Jar with Maven Assembly Plugin

Creating an Uber Jar with Maven Shade Plugin

You can use the maven-shade-plugin to package all dependencies into one uber-jar. It can also be used to build an executable jar by specifying the main class.


Java Developer Interview Q&As

800+ Java Interview Q&As

Top