Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to Create Build Java Project including all Dependencies Using Maven? maven-resources, maven-dependency & maven-jar Plugin

$
0
0

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.

Build Create Executable using Maven Plugins How to Create Build Java Project including all Dependencies Using Maven? maven resources, maven dependency & maven jar Plugin

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.

Step1: 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.

Please update directory location, filename and path as per your need below.

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

Step2: Right Click on Project -> Run As -> Maven Build

Maven Build in Eclipse How to Create Build Java Project including all Dependencies Using Maven? maven resources, maven dependency & maven jar Plugin

Step 3: Provide argument “clean install

Clean Install Maven Build in Eclipse How to Create Build Java Project including all Dependencies Using Maven? maven resources, maven dependency & maven jar Plugin

Step 4: You should see result something like this.

Console Result How to Create Build Java Project including all Dependencies Using Maven? maven resources, maven dependency & maven jar Plugin

Step 5: Now check target folder to check you have everything under that.

Complete Executable Java Project build by Maven Plugins How to Create Build Java Project including all Dependencies Using Maven? maven resources, maven dependency & maven jar Plugin

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.

If you enjoyed this post, help spread knowledge & subscribe to Crunchify's RSS feed.


The post How to Create Build Java Project including all Dependencies Using Maven? maven-resources, maven-dependency & maven-jar Plugin appeared first on Crunchify.
Author: Arpit Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles