maven 依赖版本冲突异常

好巧不巧,前几天刚刚复习完 maven 的内容今天就碰到 maven 报错。

起因是这样的,项目马上快要上线了,在上线之前需要跑一些 audit 去检查项目是否安全(这里主要是 outdated 的依赖检查)。总体来说需要过两个审查:

  • prisma

    https://apps.paloaltonetworks.com/apps

    不是 ORM 的那个,主要是 security check

  • sonatype

    好像就是之前提到的 sonatype nexus 家的 sonatype

    这里是 sonatype lifecycle 里提供的检查

另一个能过自然好,不过现在还是处于试运行状态,还没有上纲上线的需求:

  • snyk

    我还真的不知道 vendor 是谁,下载的就是一个 blobl 文件类型的报告

prisma 这里 java 的依赖查的很严,然后就碰到 spring-boot-autoconfigure 需要升级的事情,所以我就在 pom 下面手动加了一下 spring-boot-autoconfigure 能过审的版本,这个也是问题的触发点。

⚠️ 因为我没办法升级 spring-boot ❌

一个内部使用的依赖一旦升级 boot,就直接报错死给我看,所以不得已的情况下只能升级 spring-boot-autoconfigure 而没升级 spring-boot

总体来说是这样的,如果所有的依赖都只由 parent 去进行管理,那么所有项目里使用的版本是一致的,不会出现任何的问题。可是在一些情况下——比如说现在这个情况——需要升级特定的版本,那么就会出现依赖 A 和依赖 B 尽管使用同样的依赖,但是却用的是不同版本的问题

在这个情况下,其他的 spring-boot 相关依赖用的是未升级的 spring-boot 版本,而 autoconfigure 用的是升过级的版本,就产生了这样的报错信息:

Dependency convergence error for org.springframework.boot:spring-boot-autoconfigure:2.7.12 paths to dependency are:
+-com.company.project:ui:2.0.488-SNAPSHOT
  +-org.springframework.boot:spring-boot-autoconfigure:2.7.12
and
+-com.company.project:ui:2.0.488-SNAPSHOT
  +-org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.6.8
    +-org.springframework.boot:spring-boot-autoconfigure:2.7.11
and
+-com.company.project:ui:2.0.488-SNAPSHOT
  +-org.springframework.boot:spring-boot-devtools:2.7.11
    +-org.springframework.boot:spring-boot-autoconfigure:2.7.11
and
+-com.company.project:ui:2.0.488-SNAPSHOT
  +-org.springframework.boot:spring-boot-starter-test:2.7.11
    +-org.springframework.boot:spring-boot-test-autoconfigure:2.7.11
      +-org.springframework.boot:spring-boot-autoconfigure:2.7.11

大致理解一下就是,我声明了 spring-boot-autoconfigure 去使用 2.7.12 版本,但是其他的 spring-boot 依赖因为用的是统一的 spring-boot 版本,调用的是 2.7.11 版本,maven 就不高兴了

spring-boot-starter-test 有自己版本的 spring-boot-autoconfigure 这种情况称之为传递依赖,transitive dependency

最好情况下是统一版本,不过上面我也提了,短期内另一个项目是没办法动的(另一个团队在 support),所以这里只能用 workaround 去处理,也就是 maven 中的 exclusion

产生冲突的依赖如下:

        <dependency>
			<groupId>org.springframework.security.oauth.boot</groupId>
			<artifactId>spring-security-oauth2-autoconfigure</artifactId>
		</dependency>
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>

使用 exclude 的方案:

        <dependency>
			<groupId>org.springframework.security.oauth.boot</groupId>
			<artifactId>spring-security-oauth2-autoconfigure</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-autoconfigure</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html 中也有提过,这个效果大概是:

Project-A
   -> Project-B
        -> Project-D
              -> Project-E <!-- Exclude this dependency -->
              -> Project-F
   -> Project C

具体在操作时,org.springframework.security.oauth.boot 就会排除 autoconfigure,不去下载它

10-07 11:38