为什么 Maven 依赖项的顺序很重要? [英] Why order of Maven dependencies matter?

查看:43
本文介绍了为什么 Maven 依赖项的顺序很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前认为 Maven 依赖项的顺序无关紧要,并认为这是它的优点.这是我旧的 pom.xml 的依赖项:

I thought that the order of Maven dependencies doesn't matter before and regard this as a pro of it. And this is my old pom.xml's dependencies:

<dependencies>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.19</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
        <version>2.19</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.19</version>
    </dependency>

</dependencies>

效果很好,今天我想把 spring 依赖移到底部,这样那些球衣相关的就可以在一起了.但是后来我不能再让它工作了,我的码头抱怨:

It works well, and today I wanna move spring dependency to the bottom so that those jersey related can be together. However then I can no longer make it working, my Jetty complains:

[ERROR] Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.3.0.M1:run (default-cli) on project mtest: Execution default-cli of goal org.eclipse.jetty:jetty-maven-plugin:9.3.0.M1:run failed: A required class was missing while executing org.eclipse.jetty:jetty-maven-plugin:9.3.0.M1:run: org/apache/commons/logging/LogFactory

这真的很令人困惑,所以我必须关心依赖关系的顺序吗?我怎么知道正确的顺序?

That is really confusing, so do I have to concern about dependencies order? How do I know the correct order?

推荐答案

依赖的顺序确实很重要,因为从 2.0.9 版开始,Maven 如何解决传递依赖.摘自文档:

The order of dependencies does matter because of how Maven resolves transitive dependencies, starting with version 2.0.9. Excerpt from the documentation:

(...) 这决定了当遇到多个版本的工件时将使用哪个版本的依赖项.(...) 您始终可以通过在项目的 POM 中明确声明来保证版本.(...) 从 Maven 2.0.9 开始,声明中的顺序很重要:第一个声明获胜.

(...) this determines what version of a dependency will be used when multiple versions of an artifact are encountered. (...) You can always guarantee a version by declaring it explicitly in your project's POM. (...) since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins.

这篇关于为什么 Maven 依赖项的顺序很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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