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.
Please update directory location, filename and path as per your need below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<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>
|
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.
Have a suggestion or anything to add to this article? Chime in and share as a comment OR post it in forum.
Fuente:
No hay comentarios:
Publicar un comentario