
Are you working on enterprise level Java Project? Using Maven POM.xml
file to keep all dependancies up-to date? In your project do you have src folder
, resources folder
, lib folder
, etc? Well, what if you want to deploy this project to 3rd party client? Any other standalone hardware?
Well, there is a simple way to build and create your Java Project’s executable with Maven Plugins. Take a look at below sample Java Project.

Let’s get started and let me explain all parts of project:
CrunchifyMavenBuildPlugins
is a Maven Project. If you have Java project and wanted to convert it into Maven project then follow this tutorial.- We do have two folders.
src
andresources.
- Inside
resources
folder we do have a folder calledScripts
which contains one executable shell script file. CrunchifyMain.java
is a main starting point which hasmain(String args[])
method inside.pom.xml
file in which we will add Maven Plugins which will build executable .jar project with all included dependancies.
Step-1
Open your pom.xml
file and add below under <build>
. Note: I’ve added 3 plugins below below.
maven-resources-plugin
: The Resources Plugin handles the copying of project resources to the output directory. The main resources are the resources associated to the main source code.maven-dependency-plugin:
The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location.maven-jar-plugin:
This plugin provides the capability to build and sign jars.
Here is a complete pom.xml file. Mainly you would be interested in <build> tag.
Please update directory location, filename and path as per your need below.
<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>CrunchifyMavenBuildPlugins</groupId> <artifactId>CrunchifyMavenBuildPlugins</artifactId> <version>0.0.1-SNAPSHOT</version> <name>CrunchifyMavenBuildPlugins</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>2.15.0</version> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20140107</version> </dependency> <dependency> <groupId>axis</groupId> <artifactId>axis-saaj</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>2.0</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/Crunchify</outputDirectory> <resources> <resource> <directory>resources</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/Crunchify/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.crunchify.tutorial.CrunchifyMain</mainClass> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> <finalName>Crunchify/Crunchify</finalName> </configuration> </plugin> </plugins> </build> </project>
Step-2
Right Click on Project -> Run As -> Maven Build

Step-3
Provide argument “clean install
“

Step-4
You should see result something like this.

Step-5
Now check out the folder folder /target/Crunchify
to check everything under that.

Step-6
Now just run your project with below command $bash> java -jar Crunchify.jar
Do let me know if you have any problem building project. Enjoy and Happy Coding.
Very helpful tutorial. Thanks a lot!
Thanks, very helpful indeed.
Just a question, is that possible to ship dependencies inside the jar itself?
Thanks again
Hi Zack. Yeah. It’s possible by using maven-share-plugin.
We have published a detailed tutorial on Use “maven-shade-plugin” to Create just 1 Executable jar with all required Dependencies.
Let me know how it goes. Hope this helps.
useful
Thank you!
Why is the shell script inside resources?
Because you can consider shell script as a resource to your project.
I am getting a null pointer exception while following the same procedure.
MY project structure has sr/main/resources.
and Inside that directory there is a folder and inside that folder , there are 20 folder with each of them having 1000 files.
Any troubleshooting method?
Hi Sohil. Can you share detailed exception stacktrace?
Without log it’s kind of impossible to debug further. Thanks.
I have issue D:JavatestRepoTestAutomationtargetCrunchify>java -jar Crunchify.jar
Error: Could not find or load main class com.crunchify.tutorial.CrunchifyMain
Hi Ipa – can you help share complete stacktrace?
Hi Thanks for the great Job. But when i do this it generates in TARGET, Classes, maven-archiver and test.jar, instead of test folder with test.jar inside
Can you share screenshot Devz? I think the behaviour you saw is because of some additional jar may present in your pom.xml file.
What to add in com.crunchify.tutorial.CrunchifyMain if I dont have a main class in my project?
Hi Lakshmi – for executable .jar file you atleast need to have one mainclass. That mainclass will get executed when you run using command java -jar crunchify.jar
Straightforward and efficient!
🙂
helped me a lot, but is there a possibility to set some variables in resources according to the used build profile?
Hi zlosim – that is possible. I’ll write a new tutorial on profiles which you could use with Maven build.
Awesome, Nice article with simple steps. Thank you so much
Thanks Vinod.
Any ideas how to build the project into a Jar file, but package the Maven dependencies into a separate Jar file that can be shared with other projects?
Good point but I’m not sure if it’s possible to share lib with other project unless you hard code common lib directory in all other project. I’ll look at this in details though.
I have been dealing with this problem for a lot of days… THANK YOU VERY MUCH!!!!
Awesome. Glad it worked for you Luisetelo.