Maven jar plugin example
In this example we are going to see some of the capabilities from the maven jar plugin.
Maven is a build automation tool used mainly for java projects from apache.
You can access to the maven jar plugin here.
We are going to see some examples of the capabilities of the maven jar plugin.
For this example we use the following technologies:
- MAC OSX
- Eclipse Mars.1
- Maven3
- JDK 1.8.0_65 64bits
- Junit 4.12
- Maven jar plugin 2.6
1. Introduction
The maven jar plugin provides the capability to build jars files, if you define that your project is packaged as a jar file, maven will call implicitely to this plugin. We don´t need to define it inside pom.xml
it will be downloaded and executed when maven needs it.
Despite that, we can define it inside pom.xml
in order to control some features of the generated jar file.
The maven jar plugin has two goals defined:
jar
: Allow to package our main classes as a jar filetest-jar
: Allow to package our test classes as a jar file
The default goal is jar
, there is no need to define that goal inside pom.xml
, as we said before, maven will invoke that goal when maven needs it.
You can do several things with the maven jar plugin
- Use a default
manifest
file - Custom the
manifest
file - Include/Exclude content from jar file
- Create an additional jar file in the project
- Create a jar with test classes
Let´s see all those things in more details.
2. Example project
For this example, we are going to use a java project with maven nature that will be packaged as a jar file. Eclipse Mars comes with maven support out of the box, so you don´t have to install anything. Our project will look like this
At this point, we have an empty maven project. We are going to define the maven jar plugin inside pom.xml
in order to test the plugin capabilities.
The pom.xml
will look like this
pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.examples</groupId> <artifactId>maven-jar-plugin-example</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven jar :: example</name> <url>http://maven.apache.org</url> <properties> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> </plugin> </plugins> </build> </project>
The project has a dummy class, a dummy client class and a dummy test.
3. Use a default manifest
file
The jar plugin allow us to define a default manifest file. With that option, the file located at ${project.build.outputDirectory}/META-INF/MANIFEST.MF
will be the manifest file in the jar. You can see below a pom.xml
using this
pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.examples</groupId> <artifactId>maven-jar-plugin-example</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven jar :: example</name> <url>http://maven.apache.org</url> <properties> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <useDefaultManifestFile>true</useDefaultManifestFile> </configuration> </plugin> </plugins> </build> </project>
You can run the plugin with the mvn package
command, you will see an output result like this
output:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven jar :: example 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/2.6/maven-jar-plugin-2.6.pom [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/2.6/maven-jar-plugin-2.6.pom (6 KB at 5.5 KB/sec) [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-jar-plugin-example --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-jar-plugin-example --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-jar-plugin-example --- [INFO] Surefire report directory: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.javacodegeeks.test.MyTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.06 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ maven-jar-plugin-example --- [INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.6/maven-archiver-2.6.pom [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.6/maven-archiver-2.6.pom (5 KB at 23.3 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.8.1/plexus-archiver-2.8.1.pom [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.8.1/plexus-archiver-2.8.1.pom (5 KB at 23.9 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.3.2/plexus-io-2.3.2.pom [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.3.2/plexus-io-2.3.2.pom (3 KB at 15.4 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/commons/commons-compress/1.9/commons-compress-1.9.pom [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/commons/commons-compress/1.9/commons-compress-1.9.pom (12 KB at 47.9 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.21/plexus-interpolation-1.21.pom [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.21/plexus-interpolation-1.21.pom (2 KB at 9.3 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.9/plexus-archiver-2.9.pom [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.9/plexus-archiver-2.9.pom (5 KB at 24.7 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.4/plexus-io-2.4.pom [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.4/plexus-io-2.4.pom (4 KB at 21.7 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.6/maven-archiver-2.6.jar [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.6/maven-archiver-2.6.jar (23 KB at 90.7 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.9/plexus-archiver-2.9.jar [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.9/plexus-archiver-2.9.jar (142 KB at 321.9 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.4/plexus-io-2.4.jar [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.4/plexus-io-2.4.jar (80 KB at 274.4 KB/sec) [INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/commons/commons-compress/1.9/commons-compress-1.9.jar [INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/commons/commons-compress/1.9/commons-compress-1.9.jar (370 KB at 526.9 KB/sec) [INFO] Building jar: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/maven-jar-plugin-example-1.0.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.812 s [INFO] Finished at: 2015-12-12T20:35:48+01:00 [INFO] Final Memory: 17M/188M [INFO] ------------------------------------------------------------------------
After the execution, you will have a jar artifact generated under target
folder and inside of it under the META-INF
folder you will find a file called MANIFEST.MF
with the following content
default MANIFEST file:
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Built-By: fhernandez Created-By: Apache Maven 3.3.3 Build-Jdk: 1.8.0_65
This is a default manifest
file that the plugin has generated for you.
4. Custom manifest
file
The default contents of the manifest file is described here. Since version 2.1 the maven jar plugin uses Maven Archiver 2.1 so it no longer adds the specification
and implementation
details in the manifest file, if you want to add those details, you have to manually define it.
You can alter the default content of the manifest
file with the archive
configuration element. You can see all the possibilities in this link.
We are going to define some elements in the manifest
file. You can see below a pom.xml
using this
pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.examples</groupId> <artifactId>maven-jar-plugin-example</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven jar :: example</name> <url>http://maven.apache.org</url> <properties> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <index>true</index> <manifest> <addClasspath>true</addClasspath> </manifest> <manifestEntries> <javacodegeeks>maven jar plugin example</javacodegeeks> <codification>${project.build.sourceEncoding}</codification> <key>value from javacodegeeks author</key> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build> </project>
As you can see, we define some keys inside of manifestEntries
tag like javacodegeeks as maven jar plugin example
, codification:${project.build.sourceEncoding}
and purpose:example from javacodegeeks author
. We define inside the tag archive
the index
tag as true, so a file called INDEX.LIST
will be generated with the jar files listed inside of it.
You can run the plugin with the mvn package
command, you will see an output result like this
output:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven jar :: example 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-jar-plugin-example --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-jar-plugin-example --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-jar-plugin-example --- [INFO] Surefire report directory: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.javacodegeeks.test.MyTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.06 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ maven-jar-plugin-example --- [INFO] Building jar: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/maven-jar-plugin-example-1.0.0-SNAPSHOT.jar [INFO] JarArchiver skipping indexJar /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/classes because it is not a jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.113 s [INFO] Finished at: 2015-12-12T20:54:25+01:00 [INFO] Final Memory: 11M/245M [INFO] ------------------------------------------------------------------------
After the execution, you will have a jar artifact generated under target
folder and inside of it under the META-INF
folder you will find a file called MANIFEST.MF
with the following content
custom MANIFEST file:
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver purpose: example from javacodegeeks author Built-By: fhernandez codification: UTF-8 javacodegeeks: maven jar plugin example Created-By: Apache Maven 3.3.3 Build-Jdk: 1.8.0_65
and a INDEX.LIST file with the following content
index LIST file:
JarIndex-Version: 1.0 maven-jar-plugin-example-1.0.0-SNAPSHOT.jar META-INF META-INF/maven META-INF/maven/com.javacodegeeks.examples META-INF/maven/com.javacodegeeks.examples/maven-jar-plugin-example com com/javacodegeeks
5. Include/Exclude files
If you need to include or exclude some classes in your jar you can use the maven jar plugin to achieve it, as you can see below the following pom.xml
excludes the content of all client
folders inside of class folders.
pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.examples</groupId> <artifactId>maven-jar-plugin-example</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven jar :: example</name> <url>http://maven.apache.org</url> <properties> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <excludes> <exclude>**/client/*</exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
You can run the plugin with the mvn package
command, you will see an output result like this
output:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven jar :: example 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-jar-plugin-example --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-jar-plugin-example --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-jar-plugin-example --- [INFO] Surefire report directory: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.javacodegeeks.test.MyTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.06 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ maven-jar-plugin-example --- [INFO] Building jar: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/maven-jar-plugin-example-1.0.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.173 s [INFO] Finished at: 2015-12-12T21:14:44+01:00 [INFO] Final Memory: 18M/229M [INFO] ------------------------------------------------------------------------
After the execution, you will notice that there is not any class inside the com.javacodegeeks.client
folder.
6. Additional jars
You can generate an additional jar artifact besides the main jar artifact with the maven jar plugin. You have to define the phase and the goal in order to not override the main jar artifact generation, also the classifier
tag is important for this. You can see below a pom.xml
using this
pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.examples</groupId> <artifactId>maven-jar-plugin-example</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven jar :: example</name> <url>http://maven.apache.org</url> <properties> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>client</classifier> <includes> <include>**/client/*</include> </includes> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
You can run the plugin with the mvn package
command, you will see an output result like this
output:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven jar :: example 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-jar-plugin-example --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-jar-plugin-example --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-jar-plugin-example --- [INFO] Surefire report directory: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.javacodegeeks.test.MyTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.059 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ maven-jar-plugin-example --- [INFO] Building jar: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/maven-jar-plugin-example-1.0.0-SNAPSHOT.jar [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default) @ maven-jar-plugin-example --- [INFO] Building jar: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/maven-jar-plugin-example-1.0.0-SNAPSHOT-client.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.734 s [INFO] Finished at: 2015-12-13T12:20:57+01:00 [INFO] Final Memory: 10M/245M [INFO] ------------------------------------------------------------------------
After the execution, you will have two jars files under target
folder as you can see in the output above.
7. Generate a jar artifact with test classes
The maven jar plugin allow us to generate a jar file with the test classes. You can see below a pom.xml
using this
pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.examples</groupId> <artifactId>maven-jar-plugin-example</artifactId> <version>1.0.0-SNAPSHOT</version> <name>Maven jar :: example</name> <url>http://maven.apache.org</url> <properties> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
You can run the plugin with the mvn package
command, you will see an output result like this
output:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven jar :: example 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-jar-plugin-example --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-jar-plugin-example --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-jar-plugin-example --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-jar-plugin-example --- [INFO] Surefire report directory: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.javacodegeeks.test.MyTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ maven-jar-plugin-example --- [INFO] Building jar: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/maven-jar-plugin-example-1.0.0-SNAPSHOT.jar [INFO] [INFO] --- maven-jar-plugin:2.6:test-jar (default) @ maven-jar-plugin-example --- [INFO] Building jar: /Users/fhernandez/Documents/workspaceJavaCodeGeeks/maven jar plugin/target/maven-jar-plugin-example-1.0.0-SNAPSHOT-tests.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.109 s [INFO] Finished at: 2015-12-13T12:29:47+01:00 [INFO] Final Memory: 18M/226M [INFO] ------------------------------------------------------------------------
After the execution, you will have two jars files under target
folder as you can see in the output above. One of the jars artifact is the main class and the other one is a jar artifact with your test classes.
8. Conclusions
As you have seem with this example, the maven jar plugin allow you to do several things in order to fit your deploy requirements.
9. Download the eclipse project
You can download the full source code of this example here: maven-jar-plugin-example.zip
Hi,
As u mentioned above we can exludes the file from jar & i want to know is der any configuration to rename the included file in jar.