Some brief examples on how to use this plugin. If you want to use advanced configurations you should have a look at the documentation for Maven Archiver.
To handle archiving this version of Maven JAR Plugin uses Maven Archiver 2.5.
Note: Originally, this plugin was meant to sign JARs as well. As of version 2.3, the corresponding goals are no longer supported and users are advised to use the dedicated Maven Jarsigner Plugin instead.
If the packaging of your project is set to 'jar', this plugin is executed whenever it passes the "package" phase. You can execute it using the command below:
mvn package
In your project's target directory you'll able to see the generated jar file.
Specify a list of fileset patterns to be included or excluded by adding <includes>/<include> or <excludes>/<exclude> in your pom.xml.
<project> ... <build> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <includes> <include>**/service/*</include> </includes> </configuration> </plugin> ... </plugins> </build> ... </project>
Note that the patterns need to be relative to the path specified for the plugin's classesDirectory parameter.
Specify a list of fileset patterns to be included or excluded by adding <includes>/<include> or <excludes>/<exclude> and add a classifier in your pom.xml.
Note: the jar-plugin must be defined in a new execution, otherwise it will replace the default use of the jar-plugin instead of adding a second artifact. The classifier is also required to create more than one artifact.
<project> ... <build> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>client</classifier> <includes> <include>**/service/*</include> </includes> </configuration> </execution> </executions> </plugin> ... </plugins> </build> ... </project>
When you want to create a jar containing test-classes, you would probably want to reuse those classes. There are two ways to solve this:
You can produce a jar which will include your test classes and resources.
<project> ... <build> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> ... </plugins> </build> ... </project>
To reuse this artifact in an other project, you must declare this dependency with classifier test-jar :
<project> ... <dependencies> <dependency> <groupId>groupId</groupId> <artifactId>artifactId</artifactId> <type>test-jar</type> <version>version</version> <scope>test</scope> </dependency> </dependencies> ... </project>
Note: The downside of this solution is that you don't get the transitive test-scoped dependencies automatically. Maven only resolves the compile-time dependencies, so you'll have to add all the other required test-scoped dependencies by hand.
In order to let Maven resolve all test-scoped transitive dependencies you should create a separate project.
<project> <groupId>groupId</groupId> <artifactId>artifactId-tests</artifactId> <version>version</version> ... </project>
Now you have your reusable test-classes and you can refer to it as you're used to:
<project> ... <dependencies> <dependency> <groupId>groupId</groupId> <artifactId>artifactId-tests</artifactId> <version>version</version> <scope>test</scope> </dependency> </dependencies> ... </project>
For full documentation, click here.