在多模块项目中,maven 模块可以访问它所依赖的另一个模块的传递测试范围依赖项吗? [英] In a multi-module project, can a maven module access transitive test-scoped dependencies of another module it depends on?

查看:24
本文介绍了在多模块项目中,maven 模块可以访问它所依赖的另一个模块的传递测试范围依赖项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多模块 Maven+Spring 项目.一些模块依赖于其他模块.

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

假设我们有一个名为 services 的模块,它依赖于名为 persistence 的模块.

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

服务模块:

  • 在 Spring 级别,导入 persistence 上下文
  • 在 Maven 级别,依赖于 persistence 模块
  • At Spring level, imports the persistence context
  • At Maven level, depends on the persistence module

persistence 模块定义了一些与...持久性相关的配置:数据源、JPA、事务...

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

它有一些用于测试数据库(JDBC 驱动程序、DBCP、H2)的依赖项,这些依赖项仅限于测试范围,因为在部署应用程序时,DataSource 将在容器(Tomcat)中定义并通过 JNDI 访问.

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.

现在,我想在 services 模块的 Maven 测试阶段访问 persistence 模块的测试范围(传递)依赖项.

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.

Maven 手册(表 3.1) 说通常情况下,测试范围的依赖关系是不可传递的.

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 ?

如果没有什么是好的选择?(在父pom中定义测试依赖?...)

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

推荐答案

我确切地找到了它应该如何工作,即通过生成一个测试 JAR,这是一种工件,在模块中被其他人用作依赖项,在我们的示例中是持久性模块:

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>

然后通过将此测试 jar 声明为另一个模块的测试范围依赖项,在我们的示例中为 services 模块:

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>

注意第二个依赖项与第一个依赖项相同,除了 type 设置为 test-jarscope设置为测试.

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.

现在,您可以想象在 service 模块中编写的测试可以访问 persistence 模块的测试类(这可行),但也适用于持久性模块的测试范围依赖项.

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.

但是,这是一个已知问题 (https://issues.apache.org/jira/browse/MNG-1378),它不能那样工作.它自 2005 年以来一直开放,所以我认为它不会在不久的将来修复......但谁知道呢.

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.

我只需要在两个模块上复制测试范围的依赖项,或者在父 pom 中定义它们...

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

这篇关于在多模块项目中,maven 模块可以访问它所依赖的另一个模块的传递测试范围依赖项吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆