Maven的依赖插件会忽略依赖版本? [英] maven dependency plugin ignores dependency versions?

查看:153
本文介绍了Maven的依赖插件会忽略依赖版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,Maven依赖插件在计算依赖项列表时行为不当。

in my opinion the maven dependency plugin is misbehaving when calculating the dependency list.

假设以下3个项目:

base1:

<?xml version="1.0" encoding="UTF-8"?>                                                                                                                            
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>mygroup</groupId>
    <artifactId>base1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
      <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.3</version>
      </dependency>
    </dependencies>
</project>

base2:

<?xml version="1.0" encoding="UTF-8"?>                                                                                                                            
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>mygroup</groupId>
    <artifactId>base2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
      <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
      </dependency>
    </dependencies>
</project>

组合:

<?xml version="1.0" encoding="UTF-8"?>                                                                                                                            
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>mygroup</groupId>
    <artifactId>combined</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
      <dependency>
        <groupId>mygroup</groupId>
        <artifactId>base1</artifactId>
        <version>1.0-SNAPSHOT</version>
      </dependency>
      <dependency>
        <groupId>mygroup</groupId>
        <artifactId>base2</artifactId>
        <version>1.0-SNAPSHOT</version>
      </dependency>
    </dependencies>
</project>

base1和base2都取决于commons-lang,但是每个版本都不同!
的总和取决于base1和base2。

Both, base1 and base2 depend on commons-lang, but each on a different version! combined depends on both, base1 and base2.

在合并时调用 mvn依赖项:list 时,我希望在版本2.3和2.6中看到base1,base2和commons-lang,因为两者都使用了。
但是实际输出是:

When calling mvn dependency:list on combined, I would expect to see base1, base2 and commons-lang in versions 2.3 and 2.6, since both are used. However the actual output is:

[INFO] The following files have been resolved:
[INFO]    commons-lang:commons-lang:jar:2.3:compile
[INFO]    mygroup:base1:jar:1.0-SNAPSHOT:compile
[INFO]    mygroup:base2:jar:1.0-SNAPSHOT:compile

它甚至没有使用版本号最高的公共语言,而只是使用它找到的一个首先。

It is not even using the common-lang with the highest version number, but just the one it finds first.

如何避免这种情况?我需要所有依赖项。

How can I avoid this? I need all dependencies.

推荐答案

根据此官方文档(相关部分以粗体突出显示):

According to this official documentation (with the relevant part highlighted in bold):


依赖关系中介-确定遇到工件的多个版本时将使用哪个版本的依赖关系。当前,Maven 2.0仅支持使用最近定义,这意味着它将使用依赖树中最接近您的项目的版本。您始终可以通过在项目的POM中明确声明版本来保证版本。 请注意,如果两个依赖关系版本在依赖关系树中的深度相同,则直到Maven 2.0.8都没有定义哪个将获胜,但是由于Maven 2.0.9的存在,因此声明中的顺序才是最重要的:

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.

因此,Maven选择2.3版,因为它是在依赖关系解析过程中首先遇到的。请注意,如果在 combined 模块上运行 mvndependency:tree ,它将显示使用了哪个版本以及哪个版本

Therefore, Maven picks version 2.3 because it is encountered first in the dependency resolution process. Note that if you run mvn dependency:tree on the combined module, it will show which version was used and which one was omitted.

最好的解决方案是通过声明 combined 工件中显式选择所需的版本。依赖于它的POM,以便Maven优于其他版本:

The best solution is to explicitly pick the version you want in the combined artifact, by declaring the dependency in its POM so that Maven favors it over other versions:

<?xml version="1.0" encoding="UTF-8"?>
<project>
   <modelVersion>4.0.0</modelVersion>
   <groupId>mygroup</groupId>
   <artifactId>combined</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>

   <dependencies>
     <dependency>
       <groupId>mygroup</groupId>
       <artifactId>base1</artifactId>
       <version>1.0-SNAPSHOT</version>
     </dependency>
     <dependency>
       <groupId>mygroup</groupId>
       <artifactId>base2</artifactId>
       <version>1.0-SNAPSHOT</version>
     </dependency>
     <dependency>    <!-- This will override the versions in base1 and base2 -->
       <groupId>commons-lang</groupId>
       <artifactId>commons-lang</artifactId>
       <version>2.6</version>
     </dependency>
   </dependencies>

请注意,Maven不能选择两个版本,因为在这种情况下,项目的类路径上将有两个相同类的定义,这可能在运行时导致意外问题。

Note that Maven cannot pick two versions because in this case there would be two definitions for the same classes on your project's classpath, which can lead to unexpected issues at runtime.

这篇关于Maven的依赖插件会忽略依赖版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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