How to Build Java Project including all Dependencies Using Maven? maven-resources, maven-dependency and maven-jar Plugins

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » How to Build Java Project including all Dependencies Using Maven? maven-resources, maven-dependency and maven-jar Plugins
build-java-project-including-all-dependencies-using-maven-maven-resources-maven-dependency-and-maven-jar-plugins

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.

crunchify-com-maven-example-complete-project-structure

Let’s get started and let me explain all parts of project:

  1. CrunchifyMavenBuildPlugins is a Maven Project. If you have Java project and wanted to convert it into Maven project then follow this tutorial.
  2. We do have two folders. src and resources.
  3. Inside resources folder we do have a folder called Scripts which contains one executable shell script file.
  4. CrunchifyMain.java is a main starting point which has main(String args[]) method inside.
  5. 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.

  1. 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.
  2. 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.
  3. 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

Maven Build in Eclipse

Step-3

Provide argument “clean install

Clean Install - Maven Build in Eclipse

Step-4

You should see result something like this.

maven-clean-install-build-success-result

Step-5

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

maven-resources-maven-dependency-maven-jar-plugin-to-build-java-project

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.

25 thoughts on “How to Build Java Project including all Dependencies Using Maven? maven-resources, maven-dependency and maven-jar Plugins”

  1. Thanks, very helpful indeed.

    Just a question, is that possible to ship dependencies inside the jar itself?
    Thanks again

    Reply
  2. 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?

    Exception in thread "main" java.lang.NullPointerException
    Reply
  3. I have issue D:JavatestRepoTestAutomationtargetCrunchify>java -jar Crunchify.jar

    Error: Could not find or load main class com.crunchify.tutorial.CrunchifyMain

    Reply
  4. 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

    Reply
    • Can you share screenshot Devz? I think the behaviour you saw is because of some additional jar may present in your pom.xml file.

      Reply
  5. What to add in com.crunchify.tutorial.CrunchifyMain if I dont have a main class in my project?

    Reply
    • 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

      Reply
  6. helped me a lot, but is there a possibility to set some variables in resources according to the used build profile?

    Reply
  7. 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?

    Reply
    • 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.

      Reply

Leave a Comment