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:

  • The packaging type of the project must be jar: <packaging>jar</packaging>
  • The <mainClass> element specifies the main class of your application in form of fully qualified name. For example, in the above XML we specify the main class is com.inventory.gui.InventoryApp

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 began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#8CHRISTOPHER JOHN TRI2023-05-17 05:18
need updates regarding java features
Quote
#7erfan2021-11-14 14:49
That actually helped me a lot, thank you!
Quote
#6Vijay2021-03-15 12:17
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (default-cli) on project xxxxxxxxx: Error reading assemblies: No assembly descriptors found. -> [Help 1]
Quote
#5Bala Subramani2019-12-13 01:35
.jar file got created, but when i try to run, no action was performed.
Quote
#4neha2019-06-01 06:10
I tried everything but doesn't work for me
Quote