具有不同范围的依赖内的Maven依赖 [英] Maven dependency within dependency with different scope

查看:118
本文介绍了具有不同范围的依赖内的Maven依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在下面的项目中定义了两个Maven依赖项。

Say I have two Maven dependencies defined in a project like below.

    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.3.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mycompany.library</groupId>
        <artifactId>mylibrary</artifactId>
        <version>1.0.1</version>
        <scope>compile</scope>
    </dependency>

然后,在mylibrary中,我还有一个如下定义的依赖项。

Then, within mylibrary, I also have a dependency defined as below.

    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.3.1</version>
        <scope>compile</scope>
    </dependency>

当我打包我的项目时,我没有看到xstream打包在其中。我认为项目的xstream依赖范围'test'覆盖了mylibrary的xstream依赖范围'compile'。

When I package my project, I don't see xstream packaged within it. I think the project's xstream dependency scope, 'test' is overriding the mylibrary's xstream dependency scope, 'compile'.

在这种情况下,最好的方法是什么整个项目的xstream所以子模块在打包到项目中时可以访问它吗?

In this kind of situation, what's the best way to include the xstream for the whole project so the submodule can have access to it when packaged within the project?

我读过Apache Maven网站关于Transitive依赖的解释,但我'我很难理解这意味着什么,也找到了这种情况下的最佳实践。

I've read Apache Maven website's explanation on Transitive dependencies, but I'm struggling to understand what it means, and also to find out the best practice in this situation.

推荐答案

这感觉很奇怪我,如果它是特色,我认为这是一个非常危险的。
无论如何,它不是Maven错误,而是在maven文档中这里

This feels really odd to me, and if it's "feature", I think it is a really dangerous one. Anyway, it's not a Maven bug and it's in the maven documentation here.

关于这个问题的最佳实践,我没有听说过,但最安全的方法应该是完全从你的pom中删除xstream,依赖于传递依赖。如果删除了对mylibrary的依赖,则执行此操作将导致构建失败。这将作为通知您需要修复的东西。你不会默默地放弃所需的依赖关系,你也不会默默地拥有你不再需要的依赖关系。

Regarding best practices on this issue, I haven't heard of any, but the safest way to proceed ought to be to entirely remove xstream from your pom, relying on the transitive dependency. Doing this will result in a build failure if the dependency to mylibrary is removed. This will act as a notification to you that you need to fix something. You won't silently loose required dependencies, and you won't silently have dependencies you no longer need.

在旁注中,mvn依赖:analyze可用于检查包含但未使用的依赖项。

On a side note, mvn dependency:analyze can be used to check for dependencies that are included but not used.

这篇关于具有不同范围的依赖内的Maven依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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