Saturday 1 October 2022

Create a fat jar using maven assembly plugin

Maven assembly plugin enable developers to generate a fat jar that can contains dependencies, modules, site documentation, and other files.

 

At the time of writing this post, Maven assembly plugin can create distributions in the following formats:

 

a.   zip

b.   tar

c.    tar.gz (or tgz)

d.   tar.bz2 (or tbz2)

e.   tar.snappy

f.     tar.xz (or txz)

g.   jar

h.   dir

i.     war

 

How to configure assembly plugin in pom.xml?

Maven assembly plugin block looks like below.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>2.6</version>
	<configuration>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<mainClass>com.sample.app.App</mainClass>
			</manifest>
		</archive>
	</configuration>
	<executions>
		<execution>
			<id>assemble-all</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>

 

Follow below step-by-step procedure to build a fat jar using assembly plugin.

 

Step 1: Create new maven project ‘assembly-plugin-demo’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<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.sample.app</groupId>
	<artifactId>assembly-plugin-demo</artifactId>
	<version>1</version>

	<properties>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.source>1.8</maven.compiler.source>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.9.1</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>assemble-all</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

 

Step 3: Define main application class.

 

App.java

package com.sample.app;

import com.google.gson.Gson;

public class App {
	static class Employee {
		private int id;
		private String name;

		public Employee(int id, String name) {
			this.id = id;
			this.name = name;
		}

	}

	public static void main(String[] args) {
		Employee emp = new Employee(1, "Krishna");
		Gson gson = new Gson();
		System.out.println(gson.toJson(emp));
		
	}
}

 

Total project structure looks like below.



How to get a fat jar from maven project?

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

It generates jar file (assembly-plugin-demo-1-jar-with-dependencies.jar ) in the folder target.

 

How to run the jar file?

java -jar {jar_file}

$java -jar ./target/assembly-plugin-demo-1-jar-with-dependencies.jar 
{"id":1,"name":"Krishna"}

You can download the application from this link.




Previous                                                 Next                                                 Home

No comments:

Post a Comment