java - 我应该如何排除提供的 jar 的错误版本?

标签 java maven dependencies

我遇到了一些 jar 版本冲突的问题。我依赖于库 group-a:artifact-a:0.0.1,它依赖于 group-b:artifact-b:0.0.1,但是我不希望包含 group-b:artifact-b:0.0.1,因为我知道在运行时会有 group-b:artifact-b:0.0。 2

我必须如何编写 pom.xml 文件?

以下哪一项是正确的?它们之间有什么区别?

解决方案一:

group-a:artifact-a:0.0.1 中排除 group-b:artifact-b:

<dependencies>
    <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-a</artifactId>
        <version>0.0.1</version>
        <exclusions>
            <exclusion>
                <groupId>group-b</groupId>
                <artifactId>artifact-b</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

解决方案 2:

添加 group-b:artifact-b 依赖项作为提供:

<dependencies>
    <dependency>
        <groupId>group-b</groupId>
        <artifactId>artifact-b</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-a</artifactId>
        <version>0.0.1</version>
    </dependency>
</dependencies>

解决方案 3:

添加 group-b:artifact-b 依赖作为运行时:

<dependencies>
    <dependency>
        <groupId>group-b</groupId>
        <artifactId>artifact-b</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-a</artifactId>
        <version>0.0.1</version>
    </dependency>
</dependencies>

解决方案 4:

添加 group-b:artifact-b 依赖作为providedruntime 并将其从 group-a:artifact- 中排除a:0.0.1.

更新: 抱歉没有明确说明,但是是的,@Tunaki 的假设是正确的,编译时不需要依赖项 group-b:artifact-b:0.0.2

最佳答案

选项 5:只需在 POM 中明确声明版本。

如果您在 pom.xml 中显式声明它们,Maven 将覆盖传递依赖项。您不需要对提供的或运行时范围进行任何处理。

根据docs (强调我的):

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.

所以你的 pom 应该是:

<dependencies>
    <dependency>
        <groupId>group-b</groupId>
        <artifactId>artifact-b</artifactId>
        <version>0.0.2</version>
    </dependency>
    <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-a</artifactId>
        <version>0.0.1</version>
    </dependency>
</dependencies>

关于java - 我应该如何排除提供的 jar 的错误版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32275646/

相关文章:

windows - %M2% 在系统变量 Path 中不起作用,但仅在用户变量 Path 中起作用

spring-boot - 使用 Gradle 5.1 "implementation platform"代替 Spring 依赖管理插件

Java 1.8、tomcat 8.5、Jersey 2 - 无法正常工作

Java异常和错误处理

android - 如何向 Android 应用提供 Jacoco 代理?

java - 有没有办法使用 Apache poi 从给定的 PowerPoint 文件中准确获取演讲者笔记?

java - org.apache.http 库缺少包

python - 将需求从 requirements.txt 添加到 conda meta.yaml

超出 Java GC 开销限制。大量的 HashMap 和优先级队列。我能做些什么?

java - 计算Java中的曼哈顿距离