java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > maven依赖版本冲突

maven依赖版本冲突如何处理

作者:百万彩票中奖候选人

文章主要介绍了Maven依赖版本冲突的原因以及如何处理版本冲突的方法,包括使用exclusions排除依赖和使用dependencyManagement锁定版本号

maven依赖版本冲突处理

maven依赖版本冲突一般是由于间接依赖导致一个jar包有多个不同的版本,

比如:A依赖了B的1.0版本,C依赖了B的2.0版本,项目依赖A和C从而间接依赖了B的1.0和2.0版本,此时B有两个版本引入到了项目中,当存在版本冲突时可能会出现ClassNotFoundException、NoSuchMethodError等错误。

处理版本冲突可以使用以下方法:

方法1 : 使用 exclusions 排除依赖

比如:我们只依赖B的1.0版本,此时可以在依赖C时排除对B的依赖

 <!-- 排除 Spring Boot 依赖的日志包冲突 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

方法2 : 使用 dependencyManagement 锁定版本号

通常在父工程对依赖的版本统一管理。

比如:我们只依赖B的1.0版本,此时可以在父工程中限定B的版本为1.0.

	<packaging>pom</packaging>
	<properties>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文