如何在dependencyManagement中替换springboot相关的所有组件的版本?

项目的结构如下
image.png
问题是:雇主要求要升级spring-core的版本,我这里想着是把spring-boot-starter-parent版本升级
我的操作是:在A项目中在<dependencyManagement>中加入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.6</version>
    <scope>import</scope>
    <type>pom</type>
</dependency>

结果是:springboot的各种starter的版本没有变,springframework的版本变成了我想要的,这样会导致boot的版本和spring的版本不匹配
我想要的结果是:springboot和spring的版本一起变父工程B不动(不知道是哪个项目组负责的)

阅读 1.4k
avatarAI BotBETA

在 Maven 中,父 POM 的 <dependencyManagement> 元素会覆盖子 POM 中的 <dependencies> 元素。这意味着,如果你在父 POM 中定义了 spring-boot-starter-parent,它将覆盖子 POM 中的所有 Spring Boot 依赖项的版本。

你的问题在于,你只升级了 Spring Core 的版本,而没有升级 Spring Boot 的版本,这导致 Spring Boot 的版本和 Spring Core 的版本不匹配。

要解决这个问题,你应该在父 POM 中同时引入 spring-boot-starter-parentspring-core 的依赖项,并确保它们的版本匹配。然后,你可以在子 POM 中引入具体的 Spring Boot Starter,如 spring-boot-starter-web 等。这样,Spring Boot 和 Spring Core 的版本就会匹配。

以下是你可以在父 POM 中添加的依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>2.6.6</version>
    </dependency>
</dependencies>

然后,在子 POM 中引入具体的 Spring Boot Starter:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

这样,所有的 Spring Boot Starter 都会使用 spring-boot-starter-parent 中定义的版本,而 spring-core 也会使用相同的版本。只有当你明确指定某个依赖项的版本时,才会覆盖这个版本。

1 个回答

<dependencyManagement>只是一个版本仲裁

生效顺序:自定义版本 优先于 统一版本仲裁(dependencyManagement)

如果想统一指定parent中的版本,引入的时候不写版本即可,不写版本就会引用parent的spring-boot-dependencies中默认的版本

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题