本文介绍了Maven:测试与传递编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我对jar A有一个编译依赖关系,而jar A对B有一个编译依赖关系.此外,我的测试对B有一个依赖关系.

Let's say I have a compile dependency on jar A which has a compile dependency on B. Furthermore, my tests have a dependency on B.

当我声明对B的测试"依赖项时,这似乎覆盖了对B的传递性编译依赖项,因此我不能再编译我的项目了.如果我不使用B,那么一切都会正常,但是会感到奇怪,因为我直接使用了传递依赖.

When I declare a "test" dependency on B, this seems to override the transitive compile dependency on B, so that I cannot compile my project anymore. If I leave out B, everything works, but it feels strange because I use a transitive dependency directly.

什么是类似于Maven"的解决方案?

What would be a "Maven-like" solution for this?

示例:

如果我只有

<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
</dependency>

然后我的依赖项:列表为:

then my dependency:list is:

commons-codec:commons-codec:jar:1.2:compile
commons-httpclient:commons-httpclient:jar:3.1:compile
commons-logging:commons-logging:jar:1.0.4:compile

树是:

      --- maven-dependency-plugin:2.8:tree (default-cli) @ testcompile ---
      de.continentale.spu:testcompile:jar:0.0.1-SNAPSHOT
      \- commons-httpclient:commons-httpclient:jar:3.1:compile
         +- commons-logging:commons-logging:jar:1.0.4:compile
         \- commons-codec:commons-codec:jar:1.2:compile

如果我使用

<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
</dependency>
<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.0.4</version>
  <scope>test</scope>
</dependency>

然后我得到了依赖项:list

Then I get the dependency:list

commons-codec:commons-codec:jar:1.2:compile
commons-httpclient:commons-httpclient:jar:3.1:compile
commons-logging:commons-logging:jar:1.0.4:test

树是:

--- maven-dependency-plugin:2.8:tree (default-cli) @ testcompile ---
de.continentale.spu:testcompile:jar:0.0.1-SNAPSHOT
+- commons-httpclient:commons-httpclient:jar:3.1:compile
|  \- commons-codec:commons-codec:jar:1.2:compile
\- commons-logging:commons-logging:jar:1.0.4:test

所以commons-logging改变了它的范围,不再可用于编译.

So commons-logging changed its scope and is not available any more for compilation.

如果我使用src/main/java中commons-logging中的任何内容,则第一个版本会编译,而第二个版本不会编译.额外的测试依赖项隐藏"了传递编译依赖项.

If I use anything from commons-logging in the src/main/java, then the first version compiles while the second does not. The additional test dependency "hides" the transitive compile dependency.

推荐答案

虽然没有遇到过这种情况,但是如果我理解您的问题,那就是测试所要求的依赖B的版本和依赖A所要求的版本.如果版本相同,则不会遇到此问题.

Have not come across such a situation but if I understood your problem which is the version of dependency B required by your tests and required by dependency A would be different. If the versions are same then you would not have face this issue.

在这种情况下,您可以使用Maven提供的Dependency Scope功能.让依赖项A获得其所需版本的依赖项B,并在pom中用范围test声明一个单独的依赖项B.这意味着其用法将仅限于测试.您可以在此处

In such case, you could use Dependency Scope feature provided by Maven. Let dependency A get the dependency B of its required version and declare a separate dependency B in your pom with a scope test. That means its usage will be restricted to tests only. You can know more here

这篇关于Maven:测试与传递编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:04