Showing posts with label jar. Show all posts
Showing posts with label jar. Show all posts

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

Tuesday, 14 December 2021

How to specify jar files in command line?

Java takes the current directory as default classpath, you can customize this using -cp option.

 

Syntax

In Linux and MAC systems

javac -cp jar1:jar2:jar3:dir1:dir2:. HelloWorld.java
java -cp jar1:jar2:jar3:dir1:dir2:. HelloWorld

 

In Windows systems

javac -cp jar1;jar2;jar3;dir1;dir2;. HelloWorld.java
java -cp jar1;jar2;jar3;dir1;dir2;. HelloWorld

 

Let’s see it with an example.

 

PrettyJson.java

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class PrettyJson {

	public static void main(String args[]) {
		String uglyJson = "{\"name\": \"Krishna\", \"age\": 31}";

		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		JsonElement jsonElement = JsonParser.parseString(uglyJson);
		String prettyJsonString = gson.toJson(jsonElement);

		System.out.println(prettyJsonString);
	}

}

 

In my case gson-2.8.9.jar library is located at '/Users/Shared/jars/' folder.

$javac -cp /Users/Shared/jars/gson-2.8.9.jar:. PrettyJson.java 
$
$java -cp /Users/Shared/jars/gson-2.8.9.jar:. PrettyJson
{
  "name": "Krishna",
  "age": 31
}

 


 

   

You may like

Interview Questions

volatile reference vs Atomic references defined in java.util.concurrent.atomic package

Check whether I have JDK or JRE in my system

How to check whether string contain only whitespaces or not?

How to call base class method from subclass overriding method?

Can an interface extend multiple interfaces in Java?