Repackage Tests Maven Plugin

A Maven plugin to transform a test-jar to a "normal" jar

License

License

Categories

Categories

Maven Build Tools
GroupId

GroupId

org.l2x6.rpkgtests
ArtifactId

ArtifactId

repackage-tests-maven-plugin
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

Repackage Tests Maven Plugin
A Maven plugin to transform a test-jar to a "normal" jar
Project URL

Project URL

https://github.com/ppalaga/repackage-tests-maven-plugin
Source Code Management

Source Code Management

https://github.com/ppalaga/repackage-tests-maven-plugin

Download repackage-tests-maven-plugin

How to add to project

<plugin>
    <groupId>org.l2x6.rpkgtests</groupId>
    <artifactId>repackage-tests-maven-plugin</artifactId>
    <version>0.1.0</version>
</plugin>

Dependencies

compile (2)

Group / Artifact Type Version
org.apache.maven : maven-plugin-api jar 3.3.9
org.twdata.maven : mojo-executor jar 2.3.0

provided (2)

Group / Artifact Type Version
org.apache.maven : maven-core jar 3.3.9
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.5

Project Modules

There are no modules declared in this project.

rpkgtests-maven-plugin

This Maven plugin makes it possible to run tests from third party test jars outside their original source tree.

The problem

Say that a third party project publishes their test-jars on Maven Central and you want to run their tests as a part of your own build in a fully independent source tree.

You hope to make that possible by adding those test-jars as a dependency in the module where you want to run the included tests. But you will find our soon that Surefire/Failsafe Maven plugin cannot run those tests seamlessly. The build will most probably fail due to dependencies missing in the class path of the executed tests. Closer look will reveal that the missing dependencies are exactly the ones having the test scope in the POM of the test module of the third party project. This is caused by the fact that Maven, by design, does not (transitively) pull test dependencies for you - see https://issues.apache.org/jira/browse/MNG-1378

There are several solutions to this problem.

The "ban test scope" solution

Simply removing <scope>test</scope> from all dependencies on the side of the third party project would make your build work. It is rather unlikely that the third party project will be ready to do this for you.

The "split tests from the test app" solution

Another solution (on the third party project side) is to put the test classes into src/main/java directory of a separate Maven module. Good luck with asking the third party project to do this for you.

"Synthetic tests modules" solution

That’s what the rpkgtests-maven-plugin implements. All necessary steps happen on your side, so you’ll be able to run any third party tests without needing to do anything special on the third party side. The plugin downloads the test-jars (specified via the testJars mojo parameter) and the associated POMs and does the following modifications with them:

  • Renames the module by appending -rpkgtests to the original artifactId

  • Removes all non-test dependencies

  • Removes the test scope from every remaining dependency

  • Adds the dependency on the original artifact (without the -rpkgtests suffix)

The resulting pom.xml files are then installed along with the original test-jars into the local Maven repository. And your module in which you’d like to run them can seamlessly use them as test scoped dependencies.

Configuration

<plugin>
  <groupId>org.l2x6.rpkgtests</groupId>
  <artifactId>rpkgtests-maven-plugin</artifactId>
  <version><!-- Check the latest via http://central.maven.org/maven2/org/l2x6/rpkgtests/rpkgtests-maven-plugin/ --></version>
  <executions>
    <execution>
      <id>rpkgtests</id>
      <goals>
        <goal>rpkgtests</goal>
      </goals>
      <phase>generate-resources</phase>
      <configuration>
        <testJars>
          <testJar>
            <!-- The test-jar will be installed
                 along with the transformed POM as
                 org.my-org:my-artifact-rpkgtests:1.2.3
                 in the local Maven repository -->
            <groupId>org.my-org</groupId>
            <artifactId>my-artifact</artifactId>
            <version>1.2.3</version>
          </testJar>
        </testJars>
      </configuration>
    </execution>
  </executions>
</plugin>

Versions

Version
0.1.0