DEBUG.WIKI
ZgoCloud:多地区部署,按需选节点;日本、美国、香港、德国
V.PS:让项目靠近用户,亚太、欧洲、北美 KVM VPS
晚安云;香港 CN2 ¥15/月

解决 Maven 编译报错 Dependency convergence error 依赖版本分歧

分析 maven-enforcer-plugin 开启严格依赖校验报 convergence 诱因,详解 dependencyManagement 统一版本收敛。

问题现象

在大型 Spring Cloud 工程执行 mvn compile 时,Enforcer 插件阻断构建:

[ERROR] Rule 0: org.apache.maven.plugins.enforcer.DependencyConvergence failed with message:
[ERROR] Dependency convergence error for org.slf4j:slf4j-api:1.7.36 paths to dependency are:
+-com.example:my-service:1.0.0
  +-org.springframework.boot:spring-boot-starter-web:2.7.0
    +-org.slf4j:slf4j-api:1.7.36
and
+-com.example:my-service:1.0.0
  +-com.alibaba:druid-spring-boot-starter:1.2.8
    +-org.slf4j:slf4j-api:1.7.25

触发根因

项目开启了 DependencyConvergence 严格规则,严禁项目中出现同一个依赖的多个不同次版本冲突。

解决方案

在根 POM 中通过 dependencyManagement 强制锁定版本

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
    </dependencies>
</dependencyManagement>

验证方法

再次执行构建,所有传递依赖强制收敛至统一版本,构建标绿通过。