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

查看:471
本文介绍了为什么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>

效果很好,今天我想把对弹簧的依赖移到最底下,以便那些与球衣有关的人可以在一起.但是后来我再也无法正常工作了,我的码头抱怨道:

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如何解决传递性依赖项.摘录自文档:

(...)这确定遇到工件的多个版本时将使用哪个版本的依赖项. (...)您始终可以通过在项目的POM中显式声明版本来保证版本. (...)自Maven 2.0.9起,声明中的顺序才算:第一个声明获胜.

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

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