如何解决Maven中的依赖冲突? [英] How do I resolve a dependency conflict in Maven?

查看:116
本文介绍了如何解决Maven中的依赖冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的旧项目,正在向其中添加组件.该组件使用 HtmlUnit .我可以用Maven编译好它,但是当我运行它时,我得到了:

I have a fairly large legacy project that I'm adding a component to. This component uses HtmlUnit. I can compile it ok with Maven but when I run it I get:

java.lang.NoSuchMethodError:
  org.apache.http.conn.ssl.SSLConnectionSocketFactory.<init>
    (Ljavax/net/ssl/SSLContext;[Ljava/lang/String;[Ljava/lang/String;Ljavax/net/ssl/HostnameVerifier;)

因此它缺少正确的构造函数.我认为这几乎可以肯定是httpclient中的版本冲突,但是我不确定如何解决它.这是我的pom.xml的相关部分(请注意我一直尝试使用排除和依赖管理的所有游戏):

So it's missing the correct constructor. I think this is almost certainly a version conflict in httpclient but I'm not sure how to resolve it. Here's the relevant part of my pom.xml (note all the games I've been trying to play with exclusions and dependency management):

<dependencies>
        <dependency>
            <groupId>com.mycompany.mine</groupId>
            <artifactId>my-base-project</artifactId>
            <version>${project.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpclient</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>base-project</artifactId>
            <version>${project.version}</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpclient</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.2</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

有什么想法吗?

编辑:建议该问题与此问题重复,但并非如此因为在这种情况下依赖类型不是war.

Edit: it's been suggested that this question is a duplicate of this one, but it's not since the dependency type in this case is not war.

推荐答案

为了识别冲突的依赖关系,请使用mvn dependency:tree.我喜欢将其通过管道传输到文本文件以便于使用:

In order to identify conflicting dependecies, use mvn dependency:tree. I like to pipe it to a text file for ease of use:

mvn dependency:tree > tree.txt

然后,使用您喜欢的文本编辑器来查找多种版本.

Then, use your favorite text editor to look for multiple versions of a depedency.

或者,如果您要查找特定的groupId或artifactId,请使用-Dincludes标志:

Alternatively, if you are looking for a specific groupId or artifactId, use the -Dincludes flag:

mvn dependency:tree -Dincludes=<groupId>:<artifactId>:<version>:<packaging>
mvn dependency:tree -Dincludes=org.springframework <<< get all dependencies with by groupId
mvn dependency:tree -Dincludes=:spring-web <<< get all dependencies by artifactId

您可能还想在此处添加-Dverbose标志.

You might also want to add the -Dverbose flag here.

要解决依赖关系冲突,有两种方法:

To resolve dependency conflicts, there are two ways:

1)排除不想要的人

<depdency>
    <groupId>some.stuff</groupId>
    <artifactId>with.transitive.depdency</artifactId>
    <exclusions>
        <exclusion>
            <groupId>something</groupId>
            <artifactId>unwanted</artifactId>
        <exclusion>
    <exclusions>
<depdency>

通过这种方式,您将不得不排除所有引入可传递依赖的依赖.因此,我更喜欢另一个.

With this way, you will have to exclude on every dependency that brings in a transitive one. For this reason I like the other one better.

2)明确添加所需的版本

2) Explicitly add the version you want

<dependency>
    <groupId>something</groupId>
    <artifactId>with.version.conflict</artifactId>
    <version>what I want</version>
</dependency>

这将确保所有传递依赖项都将与此精确版本交换.但是,如果某些框架实际上需要较旧的版本,则这也可能导致错误.为了安全地使用此策略,您的依赖项必须与最新的可用版本(或同时发布的版本)相当接近.

This will make sure that any transitive dependency will be swapped with this exact version. This might also lead to errors though, if some framework actually needs an older version. For using this strategy safely, your dependencies will need to be fairly close to the newest available version (or versions released at the same time).

这篇关于如何解决Maven中的依赖冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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