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.