How to create jar file with dependencies in Maven?

In this tutorial we are going to use the Maven plugin which will allows us to create jar file with all dependencies included in it.

How to create jar file with dependencies in Maven?

Creating Jar with all dependencies - How to create jar file with dependencies in Maven?

Usually developers are creating the jar files of their own code without including other libraries. Such jar/war file requires dependent libraries to be present or included in the CLASSPATH which running the code.

In this tutorial we are going to teach you about the maven plugin which allows you to include all dependencies in one jar file along with compiled classes of your project. Such jar file is useful when you want to include all the dependencies with your jar so that it can run without further setup.

We are using maven-shade-plugin which is used to create fat jar file. The fat jar file is the jar file which packaged with all dependent dependencies.  This plugin is used to create jar file which includes all the dependencies and project resources in one jar file.

Maven tool is used to find all the dependencies used in a project and the maven-shade-plugin is enhances the capability of maven by allowing packing all dependent jar files into one jar file which also includes compiled classes of the current project.

How to create jar file with dependencies in Maven?

Steps to crate jar file with all dependency are:

a) Create Maven project

b) Add dependency packages

c) Write your code

d) Add maven-shade-plugin  into pom.xml file

c) Package your project with mvn package command

We are developing a simple program which uses Google Gson library to convert list into JSON string. Here is the example code used in the project:

package net.roseindia;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/*
 * Web: http://www.roseindia.net
 * 
 */

public class ListToJson {
	public static void main(String[] args) {
		List lst = new ArrayList();
		lst.add("One");
		lst.add("Two");
		lst.add("Three");
		lst.add("Four");
		lst.add("Five");
		
		Gson gson = new GsonBuilder().disableHtmlEscaping().create();
		String jsonString = gson.toJson(lst);
		System.out.println(jsonString);		

	}
}

Our maven pom.xml file uses following dependencies:

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

Above dependency code downloads dependencies of Google Gson library.

Here is the code of maven-shade-plugin which generates jar file with all dependencies:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>shade</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<finalName>app</finalName>
	</configuration>
</plugin>

You can specify the name of jar file in the <fileName>..</fileName> tag, in the above tag I have added app as fileName. Maven will generate app.jar with all the dependencies on executing mvn package command.

Here is full code of pom.xml file used  in project:

<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 
	  http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>JarWithDependency</groupId>
	<artifactId>JarWithDependency</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.4</version>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>

		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-shade-plugin</artifactId>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>shade</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<finalName>app</finalName>
			</configuration>
		</plugin>

		</plugins>
	</build>
</project>

To create package run the following command:

mvn package

This will create app.jar in target directory of maven.

Here is sample output of mvn package command:


C:\ittraining\Workspace\JarWithDependency>mvn package
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
    JarWithDependency:JarWithDependency:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-shade-plugin is missing.
    @ line 27, column 11
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building JarWithDependency 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ JarWithDependency ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\ittraining\Workspace\JarWithDependency\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ JarWithDependency ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ JarWithDependency ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\ittraining\Workspace\JarWithDependency\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ JarWithDependency ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ JarWithDependency ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ JarWithDependency ---
[INFO]
[INFO] --- maven-shade-plugin:3.1.1:shade (default) @ JarWithDependency ---
[INFO] Including com.google.code.gson:gson:jar:2.8.4 in the shaded jar.
[INFO] Replacing C:\ittraining\Workspace\JarWithDependency\target\app.jar with 
   C:\ittraining\Workspace\JarWithDependency\target\JarWithDependency-0.0.1-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.802 s
[INFO] Finished at: 2018-05-12T21:46:16+05:30
[INFO] Final Memory: 9M/129M
[INFO] ------------------------------------------------------------------------
C:\ittraining\Workspace\JarWithDependency>

To run the application go to target directory and then run following command:

java -cp ./app.jar net.roseindia.ListToJson

This will give following output:

C:\ittraining\Workspace\JarWithDependency\target>java -cp ./app.jar net.roseindia.ListToJson
["One","Two","Three","Four","Five"]

In this tutorial you learned how to create a fat jar file which includes all the dependencies of the project.

Download application project.

Check more maven tutorials at Maven Tutorials.