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

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

问题描述

我有一个多模块的Maven + Spring项目。有些模块取决于其他模块。



假设我们有一个名为 services 的模块,取决于名为 persistence



服务模块:




  • 在Spring级别,导入持久性上下文

  • 在Maven级别,取决于持久化模块



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



它具有一些依赖关系,用于测试DB(JDBC驱动程序,DBCP,H2)限于测试范围,因为当应用程序部署时,DataSource将在容器(Tomcat)中定义并通过JNDI进行访问。



现在,我想要在服务模块的Maven测试阶段访问持久性的测试范围(传递)依赖性模块。



<啊ref =http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-project-dependencies.html =noreferrer> Maven手册(表3.1)说通常情况下,测试范围的依赖关系是不可行的。



有可能在多模块项目的上下文中获取它们吗?



如果不是什么好的选择? (定义父pom中的测试依赖关系...)

解决方案

我发现它应该如何工作, strong>生成一个测试JAR ,这是一种类型的工件,在模块中被用作另一个依赖,在我们的示例中,持久性模块:

 <建立> 
< plugins>

<! - 生成测试罐 - >
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-jar-plugin< / artifactId>
< version> 2.4< / version>
<执行>
< execution>
< goals>
< goal> test-jar< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>

< / plugins>
< / build>

然后通过将此测试jar声明为另一个模块的测试作用域依赖关系,在我们的示例中, code>服务模块:

 <! - 服务模块 - > 
<依赖关系>
< groupId> $ {project.groupId}< / groupId>
< artifactId> services< / artifactId>
< version> $ {project.version}< / version>
< / dependency>
<依赖关系>
< groupId> $ {project.groupId}< / groupId>
< artifactId> services< / artifactId>
< version> $ {project.version}< / version>
< type> test-jar< / type>
< scope> test< / scope>
< / dependency>

请注意与第一个相同的第二个依赖项,除了类型设置为 test-jar 和设置为test的范围 / p>

现在,您可以想像,在服务模块中编写的测试将可以访问测试类的持久性模块这个工作),而且还有持久性模块的测试范围依赖关系。



但是,这是一个已知问题( https:/ /issues.apache.org/jira/browse/MNG-1378 ),它不会这样工作。自2005年以来一直开放,所以我不久以后看不到它...但是谁知道。



我只需要复制测试 - 对这两个模块的范围依赖,或者只是在父进程中定义它们。


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 ?... )

解决方案

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...

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

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