In a multi-module project, can a maven module access transitive test-scoped dependencies of another module it depends on?

Pierre Henry picture Pierre Henry · Apr 4, 2013 · Viewed 9.8k times · Source

I have a multi-module Maven+Spring project. Some modules depend on other modules.

Let's say we have a module named services that depends on the module named persistence.

The services module :

  • At Spring level, imports the persistence context
  • At Maven level, depends on the persistence module

The persistence module defines some configuratrion related to the... persistence : datasource, JPA, transactions...

It has some dependencies for testing the DB (JDBC drivers, DBCP, H2) that are limited to the test scope, since when the app is deployed, the DataSource will defined in the container (Tomcat) and accessed via JNDI.

Now, I would like to have access, during the Maven test phase of the services module, to the test-scoped (transitive) dependencies of the persistence module.

The Maven manual (Table 3.1) say that normally, test-scope dependencies are not available transitively.

Is it possible to obtain them somehow in the context of a multi-module project ?

If not what are good alternatives ? (Define the test dependencies in the parent pom ?... )

Answer

Pierre Henry picture Pierre Henry · Apr 16, 2013

I found exactly how it should work, i.e. by generating a test JAR, which is a type of artifact, in the module that is used as a dependency by the other, in our example the persistence module :

<build>
    <plugins>

            <!-- Generate test jar too -->
            <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>

Then by declaring this test jar as a test scoped dependency of the other module, in our example the services module :

<!-- Services module -->
<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>services</artifactId>
    <version>${project.version}</version>
</dependency>
<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>services</artifactId>
    <version>${project.version}</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>

Note the second dependency that is identical to the first except for the type that is set to test-jar and for the scope that is set to test.

Now, you would imagine that the tests written in the service module would have access to the test classes of the persistence module (this works) but also to the test scoped dependencies of the persistence module.

However, it is a known issue (https://issues.apache.org/jira/browse/MNG-1378) that it doesn't work that way. It's been open since 2005, so I don't see it fixed in a near future... but who knows.

Si I will just have to duplicate the test-scoped dependencies on both modules, or just define them in the parent pom...