JAVA – How to create an executable JAR with dependencies using Maven
Maven
- Mavenis a powerful project management tool that is based on POM (project object model). It is used for projects build, dependency and documentation.
- It simplifies the build process like ANT. But it is too much advanced than ANT.
- Current version of Maven is 3.
A JAR (Java ARchive)
- JAR is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution.
TO CREATE A JAR FILE WITH MAVEN
Create a simple Java project
Table Of Content
- Maven
- A JAR (Java ARchive)
- TO CREATE A JAR FILE WITH MAVEN
- log4j.properties
- Make it support Eclipse.
- Update Pom.xml
- pom.xml
- Update App.java
- App.java
- Working with Dependencies
- How to add dependencies in a jar
- Solution
- pom.xml
- final Jar file
- Package
- List out the dateutils.jar content
- Run it
- Where is log4j.properties?
- Create a Java project from the Maven quick start template.
- The following files and folder structure will be created.
Above folder structure is not enough, create a log4j.properties file and put it in src/main/resources/log4j.properties, just create the resources folder manually.
log4j.properties
Make it support Eclipse.
[ad type=”banner”]Update Pom.xml
Update pom.xml to declare both log4j and the jodatime dependencies, for output to a jar format, make sure the packaging is set to “jar”.
pom.xml
Update App.java
App.java
- Thus this project has two dependencies : log4j and jodatime.
Working with Dependencies
How to add dependencies in a jar
- we can put both log4j.jar and jodatime.jar inside the final.jar, but our classes are unable to call other classes which is inside the unpack log4j.jar, Java jar is designed like this, unless we can create a special class loader like one-jar plugin.
- Alternatively, use maven-assembly-plugin to extract all dependency jars into raw classes, and group it together.
- Try one-jar plugin, it will create a fat-jar, which includes the entire project’s dependencies into a single jar file.
Solution
- Copy the entire project’s dependencies to a pre-defined folder, and define the dependency class path in the jar’s manifest file.
- The updated and final pom.xml, to use maven-dependency-plugin to copy all dependencies to target/dependency-jars/ folder, and use maven-jar-plugin to add the dependency class path.
pom.xml
[ad type=”banner”]final Jar file
Package
- Review the folder structure in the target folder

A dateutils.jar is created, and the entire project runtime dependencies (excluded junit) are copied to target/dependency-jars/ folder.
List out the dateutils.jar content :
Extracts and review the content of MANIFEST.MF, the dependencies are added in the Class-Path.
[ad type=”banner”]Run it
Where is log4j.properties?
To exclude the log4j.properties in the jar file, to avoid issues like multiple log4j.properties files in classpath.
We can still pass in the log4j properties via the log4j.configuration system property like this :


