Maven Test Dependence on a Multi-Module Project

I use maven to create a project with several modules. My module 2 depends on src module 1 in the area of ​​compiling and testing module 1 in the verification area.

Module 2 -

<dependency> <groupId>blah</groupId> <artifactId>MODULE1</artifactId> <version>blah</version> <classifier>tests</classifier> <scope>test</scope> </dependency> 

It works great. Say my module 3 depends on src module 1 and tests at compile time.

Module 3 -

  <dependency> <groupId>blah</groupId> <artifactId>MODULE1</artifactId> <version>blah</version> <classifier>tests</classifier> <scope>compile</scope> </dependency> 

When I run mvn clean install , my build works up to module 3, it crashes in module 3 because it cannot solve the dependency of the test of module 1. Then I only do mvn install on module 3, go back and run mvn install on my parent pump, to make it assembly. How can i fix this?

+66
maven-2 maven-plugin
Nov 12 '09 at 21:17
source share
2 answers

I have doubts about what you are trying to do, but I assume that you want to reuse the tests you created for the project (module1) in another. As explained in the note below the Guide to Using Connected Tests :

Note that in previous releases of this guide, it was suggested that you use <classifier>tests</classifier> instead of <type>test-jar</type> . Although this currently works in some cases, it does not work properly during assembly of the JAR module reactor and any consumer if the life cycle phase before installation is involved. In this case, Maven will not allow the test JAR from the output of the reactor assembly, but from the local / remote repository. Apparently, the JARs from the repositories may be outdated or completely absent, causing build failures (see MNG-2045 ).

So, firstly, to pack the compiled tests in the JAR and deploy them for general reuse, configure the maven-jar-plugin as follows:

 <project> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> 

Then install / deploy the test JAR artifact as usual (using mvn install or mvn deploy ).

Finally, to use the test JAR, you must specify a dependency with the specified type test-jar :

 <project> ... <dependencies> <dependency> <groupId>com.myco.app</groupId> <artifactId>foo</artifactId> <version>1.0-SNAPSHOT</version> <type>test-jar</type> <scope>test</scope> </dependency> </dependencies> ... </project> 
+100
Nov 14 '09 at 9:02
source share

As for my comment on the Pascal question, I think I found a suitable answer:

 <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> <phase>test-compile</phase> </execution> </executions> <configuration> <outputDirectory>${basedir}\target</outputDirectory> </configuration> </plugin> </plugins> 

The main difference here, as you see here, is the <phase> .

I will create a test jar, and it will be available at the compilation stage of the tests, and not just after the package phase.

It works for me.

+15
Mar 28 '10 at 10:00
source share



All Articles