Generating a JAR of the test classes

There could be situations when you would want to use the test classes and resources of a project as a test dependency in another project. This is typically the case in multi-module projects, where a module depends on another module. There could be utility code in the test classes of a module that can be reused in another module.

One way to achieve this would be by creating a separate project to hold these classes. However, that is cumbersome and may be confusing as well.

Maven provides another way to achieve this.

How to do it...

  1. Open a project for which you want to create a test JAR (project-with-test-jar).
  2. Add the following plugin configuration to the pom file:
     <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

Get Apache Maven Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.