In this article, we are going to guide you how to create an executable JAR file from a Java project which uses Maven build system. It’s worth mentioning that the project contains resource files (XML configuration, images, etc) that are in a directory different from the Java source files directory. And the project also contains some dependencies as well. The following screenshot illustrates such a Java project:

Java Maven Project

 

As you can see, this Java project contains resource files like images and XML, and dependencies for Hibernate framework. So what we are going to show you is how to generate the executable JAR file of this project in a manner that the JAR file contains all the resources and dependencies (uber-JAR or fat JAR file).

And you know, creating such JAR file is not possible with Eclipse’s Export function (File > Export > Java > Runnable JAR file). But the good news is that Maven provides a great plugin that is dedicated for creating executable JAR files with resources and dependencies. This plugin is called Maven Assembly Plugin.

To use the Maven Assembly Plugin, declare the following XML code in the <plugins> section which is under the <build>section of the pom.xml file:

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<configuration>
		<archive>
			<manifest>
				<mainClass>com.inventory.gui.InventoryApp</mainClass>
			</manifest>
		</archive>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
	</configuration>
</plugin>
 

That makes the pom.xml file looks like the following:

<project ....>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.inventory</groupId>
	<artifactId>Inventory</artifactId>
	<packaging>jar</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Inventory Management</name>
	

	<dependencies>
		....
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.inventory.gui.InventoryApp</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
Here, there are two noteworthy points:



And to generate the JAR file from the project, run Maven with the goal assembly:single. For example, in the command line:

mvn clean install assembly:single

 

In Eclipse, right click on the project and select Run As > Maven build, and specify the goal assembly:single in the dialog, then click Run:

Assembly Single Goal Maven

Wait a moment for the build to complete, and then check the JAR file generated under the project’s target directory. Note that the JAR file contains all the resources and dependencies (fat JAR or uber-JAR).

References:

 

Related Tutorials:

 

Other Java Coding Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.