Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven test dependency in multi module project

I use maven to build a multi module project. My module 2 depends on Module 1 src at compile scope and module 1 tests in test scope.

Module 2 -

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

This works fine. Say my module 3 depends on Module1 src 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 runs till module 3, fails at module 3 as it couldn't resolve the module 1 test dependency. Then I do a mvn install on module 3 alone, go back and run mvn install on my parent pom to make it build. How can I fix this?

like image 814
user209947 Avatar asked Nov 12 '09 21:11

user209947


People also ask

What are test dependencies in Maven?

A test -scoped dependency is a dependency that is available on the classpath only during test compilation and test execution. If your project has war or ear packaging, a test -scoped dependency would not be included in the project's output archive.

What is multi-module project in Maven?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.


1 Answers

I have a doubt about what you are trying to do but but I'll assume you want to reuse the tests that you have created for a project (module1) in another. As explained in the note at the bottom of the Guide to using attached tests:

Note that previous editions of this guide suggested to use <classifier>tests</classifier> instead of <type>test-jar</type>. While this currently works for some cases, it does not properly work during a reactor build of the test JAR module and any consumer if a lifecycle phase prior to install is invoked. In such a scenario, Maven will not resolve the test JAR from the output of the reactor build but from the local/remote repository. Apparently, the JAR from the repositories could be outdated or completely missing, causing a build failure (cf. MNG-2045).

So, first, to package up compiled tests in a 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 should specify a dependency with a specified type of 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> 
like image 95
Pascal Thivent Avatar answered Sep 28 '22 06:09

Pascal Thivent