gpt4 book ai didi

maven - 如何在 Maven 中调试 Artifact 替换

转载 作者:行者123 更新时间:2023-12-02 22:27:19 25 4
gpt4 key购买 nike

我有一个父项目包含十几个子项目,其中一个子项目使用org.apache.httpcomponents:httpclient:jar:4.3.5,它依赖于org.apache .httpcomponents:httpcore:jar:4.3.2.

但是,httpcore 的结果版本解析为 4.2.1,而不是 4.3.2。

以下是在 Eclipse 中选中调试选项的情况下运行 dependency:tree 时的输出提取:

...
[DEBUG] Using mirror nexus (http://192.168.0.111:8081/nexus/content/groups/public) for apache.snapshots (http://repository.apache.org/snapshots).
[DEBUG] testArtifact: artifact=org.apache.httpcomponents:httpclient:jar:4.3.5:compile
[DEBUG] includeArtifact: artifact=org.apache.httpcomponents:httpclient:jar:4.3.5:compile
[DEBUG] startProcessChildren: artifact=org.apache.httpcomponents:httpclient:jar:4.3.5:compile
[DEBUG] manageArtifactVersion: artifact=org.apache.httpcomponents:httpcore:jar:4.3.2:compile, replacement=org.apache.httpcomponents:httpcore:jar:4.2.1
[DEBUG] Using mirror nexus (http://192.168.0.111:8081/nexus/content/groups/public) for apache.snapshots (http://repository.apache.org/snapshots).
...

它只显示 replacement=org.apache.httpcomponents:httpcore:jar:4.2.1,但没有说明替换的原因。父项目的pom.xml使用了相当多的依赖项,即使我可以尝试一一删除这些依赖项并检查结果,但这将非常耗时。有没有更有效的方法来调试 Artifact 替换?

<小时/>

Here几乎是 Eclipse 中选中调试选项的 dependency:tree 的完整日志。

最佳答案

从您的日志中,您可以找到以下行:

[DEBUG] com.company.xyz:xyz-integration-lib:jar:0.0.1-SNAPSHOT
[DEBUG] com.company.xyz:xyz-utils:jar:0.0.1-SNAPSHOT:compile
[DEBUG] commons-codec:commons-codec:jar:1.8:compile
[DEBUG] javax.mail:mail:jar:1.4:provided
[DEBUG] javax.activation:activation:jar:1.1.1:provided (version managed from 1.1 by org.jboss.spec:jboss-javaee-6.0:3.0.2.Final)
[DEBUG] org.apache.commons:commons-lang3:jar:3.3.2:compile
[DEBUG] junit:junit:jar:4.8.2:test
[DEBUG] com.thoughtworks.xstream:xstream:jar:1.4.7:compile
[DEBUG] xmlpull:xmlpull:jar:1.1.3.1:compile
[DEBUG] xpp3:xpp3_min:jar:1.1.4c:compile
[DEBUG] joda-time:joda-time:jar:2.4:compile
[DEBUG] org.assertj:assertj-joda-time:jar:1.1.0:test
[DEBUG] org.assertj:assertj-core:jar:1.3.0:test
[DEBUG] org.apache.httpcomponents:httpclient:jar:4.3.5:compile
[DEBUG] org.apache.httpcomponents:httpcore:jar:4.2.1:compile (version managed from 4.3.2 by org.jboss.as:jboss-as-parent:7.2.0.Final)
[DEBUG] commons-logging:commons-logging:jar:1.1.3:compile
[DEBUG] org.slf4j:slf4j-api:jar:1.7.7:compile
[DEBUG] org.slf4j:slf4j-log4j12:jar:1.7.7:compile
[DEBUG] log4j:log4j:jar:1.2.17:compile
[DEBUG] org.mockito:mockito-all:jar:1.9.5:test
[DEBUG] org.powermock:powermock-module-junit4:jar:1.5.5:test
[DEBUG] org.powermock:powermock-module-junit4-common:jar:1.5.5:test
[DEBUG] org.powermock:powermock-core:jar:1.5.5:test
[DEBUG] org.javassist:javassist:jar:3.18.1-GA:test (version managed from 3.18.2-GA by org.springframework.boot:spring-boot-dependencies:1.1.4.RELEASE)
[DEBUG] org.powermock:powermock-reflect:jar:1.5.5:test
[DEBUG] org.objenesis:objenesis:jar:2.1:test
[DEBUG] org.powermock:powermock-api-mockito:jar:1.5.5:test
[DEBUG] org.powermock:powermock-api-support:jar:1.5.5:test

您可以在其中看到 javassisthttpcore 版本因某些传递依赖项而被删除,并且 javax.activation 版本被提升一。

当多个项目依赖项依赖于同一库并且已定义对该库的不同版本的依赖项时,就会发生这种情况。这可能很烦人,因为通常您无法更改父 POM 或其依赖项影响传递依赖项版本的方式。

调解规则来自 Maven docs具体如下:

Dependency mediation - this determines what version of a dependency will be used when multiple versions of an artifact are encountered. Currently, Maven 2.0 only supports using the "nearest definition" which means that it will use the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM. Note that if two dependency versions are at the same depth in the dependency tree, until Maven 2.0.8 it was not defined which one would win, but since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins.

"nearest definition" means that the version used will be the closest one to your project in the tree of dependencies, eg. if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0

但是,您可以自己管理依赖项版本。这称为依赖管理,如相同文档所述:

Dependency management - this allows project authors to directly specify the versions of artifacts to be used when they are encountered in transitive dependencies or in dependencies where no version has been specified. In the example in the preceding section a dependency was directly added to A even though it is not directly used by A. Instead, A can include D as a dependency in its dependencyManagement section and directly control which version of D is used when, or if, it is ever referenced.

因此,您只需添加:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>bar</groupId>
<artifactId>foo</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</dependencyManagement>

到您自己的 POM 中,这将始终覆盖通过依赖中介为您的传递依赖项定义的任何版本。

关于maven - 如何在 Maven 中调试 Artifact 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26316502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com