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:
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:
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).
Nam Ha Minh 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.