简体   繁体   English

在Maven项目中添加现有依赖项的较新版本

[英]Adding a newer version of existing dependency in Maven project

For your information, I'm new to the Maven environment and not familiar with Java development. 供您参考,我是Maven环境的新手,并不熟悉Java开发。

First, I wrote a project which is using org.apache.httpcomponents:httpclient:4.3.6 . 首先,我编写了一个使用org.apache.httpcomponents:httpclient:4.3.6 It ran well and had no problem at all. 它运行良好,完全没有问题。 And I put the source code (a Java file) into another Eclipse web project which is configured with Maven. 然后,将源代码(一个Java文件)放入另一个使用Maven配置的Eclipse Web项目中。 So I added a dependency into the pom.xml and it was compiled successfully. 因此,我在pom.xml添加了一个依赖项,并成功编译了该依赖项。 But a problem popped out when I ran it. 但是当我运行它时,出现了一个问题。 It says java.lang.NoSuchFieldError . 它说java.lang.NoSuchFieldError After lots of tries and fails, I concluded that it was caused by a version problem. 经过大量尝试和失败,我得出结论认为这是由版本问题引起的。

Dependencies in pom.xml : pom.xml依赖项:

<dependencies>
    <dependency>
        <groupId>net.generalsvc</groupId>
        <artifactId>ubi.base.bin</artifactId>
        <version>2.1.0-SNAPSHOT</version>
    </dependency>
    ...
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.6</version>
    </dependency>
</dependencies>

The net.generalsvc:ubi.base.bin includes dependency of org.apache.http:httpclient:4.2.1 . net.generalsvc:ubi.base.bin包含org.apache.http:httpclient:4.2.1依赖项。 So result dependency hierarchy is like below. 因此,结果依赖性层次结构如下所示。

依赖层次

I think my code is refering the '4.2.1' version not the '4.3.6' one. 我认为我的代码引用的是“ 4.2.1”版本,而不是“ 4.3.6”版本。 How can I assign the '4.3.6' version to my Java file? 如何为Java文件分配“ 4.3.6”版本? Can I preserve the project's existing depencencies with adding the httpclient:4.3.6 for my code? 是否可以通过为代码添加httpclient:4.3.6来保留项目的现有依赖关系?

This will exclude the specific from the maven build 这将从Maven构建中排除特定的

<dependency>
    <groupId>net.generalsvc</groupId>
    <artifactId>ubi.base.bin</artifactId>
    <version>2.1.0-SNAPSHOT</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.http</groupId>
            <artifactId>httpclient</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Maven uses a measure of "distance" to decide which dependency is to be used. Maven使用“距离”度量来确定要使用哪个依赖项。 That measure is one step per pom.xml (the dependencies in your pom.xml are 1, the dependencies on those dependencies' pom are 2, etc), and the one nearer is included, so if you include in your pom.xml a version, that's what's being used. 该度量标准是每个pom.xml的第一步(您pom.xml中的依赖关系为1,对该依赖项的pom的依赖关系为2,依此类推),并且包括一个更近的距离,因此,如果在pom.xml中包含a版本,这就是所使用的。

The same dependency (same groupId and artifactId) can't be used with two different versions, so you can't preserve your dependencies' dependencies and include another version of them. 相同的依赖项(相同的groupId和artifactId)不能与两个不同的版本一起使用,因此您不能保留依赖项的依赖项并包含它们的另一个版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM