spring boot中如何升级spring框架版本

per*_*ero 9 java spring maven spring-boot

我在 maven 中使用 spring-boot 2.3.3.RELEASE 和相应的 spring-boot-starter-parent 。

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.3.3.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
 </parent>
Run Code Online (Sandbox Code Playgroud)

由于 spring4shell CVE,我想将 spring 框架升级到 5.2.20.RELEASE,而不是已经包含的 5.2.8.RELEASE。我尝试覆盖spring-framework.versionspring-boot-dependency 中的属性。

    <spring-framework.version>5.2.20.RELEASE</spring-framework.version>
Run Code Online (Sandbox Code Playgroud)

但这没有用。我还查找了 spring-boot-starter-web-2.3.3.RELEASE.pom,它具有硬编码为 5.2.8.RELEASE 的 spring-web 依赖项。

除了将所有新版本作为依赖项添加到该部分之外,还有其他方法可以升级 spring-boot 中的 spring-framework 版本吗dependencyManagement?谢谢

完整的 POM:

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.3.3.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>group</groupId>
<artifactId>app</artifactId>
<version>3.1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
  <flyway.version>4.1.2</flyway.version>
  <groovy.version>2.4.20</groovy.version>
  <spring-framework.version>5.2.20.RELEASE</spring-framework.version>
  <spring-cloud.version>Hoxton.SR7</spring-cloud.version>
  <h2.version>1.4.196</h2.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>

<dependencies>

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

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

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

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

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

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
</dependency>


<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>${groovy.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

</dependencies>
<build>
<finalName>app</finalName>
<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <includes>
      <include>**/version.json</include>
      <include>**/**.properties</include>
    </includes>
  </resource>

  <resource>
    <directory>src/main/resources</directory>
    <filtering>false</filtering>
    <includes>
      <include>**/*.*</include>
    </includes>
    <excludes>
      <exclude>**/version.json</exclude>
      <exclude>**/**.properties</exclude>
    </excludes>
  </resource>
</resources>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)

编辑: 这是 mvn dependency:tree 的一部分:

+- org.springframework.boot:spring-boot-starter-webflux:jar:2.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.3.3.RELEASE:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.2:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-reactor-netty:jar:2.3.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-web:jar:5.2.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-webflux:jar:5.2.8.RELEASE:compile
[INFO] |  \- org.synchronoss.cloud:nio-multipart-parser:jar:1.1.0:compile
[INFO] |     \- org.synchronoss.cloud:nio-stream-storage:jar:1.1.3:compile
Run Code Online (Sandbox Code Playgroud)

如果您查看其中spring-boot-starter-webflux-2.3.3.RELEASE.pom包含有问题的 spring-web 5.2.8.RELEASE,您会发现 spring 版本被硬编码为 5.2.8.RELEASE。所以spring.framework在maven中设置属性是不会有任何效果的。

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.8.RELEASE</version>
      <scope>compile</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

mvn help: effective-pom 的输出:

 <dependency>
        <groupId>org.springframework</groupId>  <!-- org.springframework:spring-framework-bom:5.2.8.RELEASE, line 126 -->
        <artifactId>spring-web</artifactId>  <!-- org.springframework:spring-framework-bom:5.2.8.RELEASE, line 127 -->
        <version>5.2.8.RELEASE</version>  <!-- org.springframework:spring-framework-bom:5.2.8.RELEASE, line 128 -->
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>  <!-- org.springframework:spring-framework-bom:5.2.8.RELEASE, line 131 -->
        <artifactId>spring-webflux</artifactId>  <!-- org.springframework:spring-framework-bom:5.2.8.RELEASE, line 132 -->
        <version>5.2.8.RELEASE</version>  <!-- org.springframework:spring-framework-bom:5.2.8.RELEASE, line 133 -->
      </dependency>
Run Code Online (Sandbox Code Playgroud)

@Inthai2002 的解决方案后编辑: 我在 pom.xml 中另外导入了一个内部 lib pom

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>internal</groupId>
                <artifactId>lib</artifactId>
                <version>4.4.0</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

            <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>
Run Code Online (Sandbox Code Playgroud)

并且这个内部lib直接导入了 spring-boot-dependency pom ,这导致 spring-framework.version 属性被忽略:

          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.3.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
Run Code Online (Sandbox Code Playgroud)

小智 5

我刚刚在干净的 m2 存储库上尝试了你的 pom(有或没有spring-framework.version属性)。没有该属性时,spring-framework 是 5.2.8,具有该属性时,是 5.2.20。你能尝试一个干净的仓库吗?

版本 X 的spring -framework-bom被硬编码到版本 X 的所有 spring 包(参见https://repo1.maven.org/maven2/org/springframework/spring-framework-bom/5.2.8.RELEASE/spring -framework-bom-5.2.8.RELEASE.pom

spring -framework.version属性被声明并用于拉取spring-boot-dependency中的spring-framework-bom并由其后代继承(参见https://repo1.maven.org/maven2/org/springframework/boot/ spring-boot-dependency/2.3.3.RELEASE/spring-boot-dependency-2.3.3.RELEASE.pom)。

spring-boot-dependencyspring-boot-starter-parent的父级(请参阅https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.3.3.RELEASE/spring -boot-starter-parent-2.3.3.RELEASE.pom)。

由于该属性是由后代继承的,因此您可以在应用程序的 pom 中覆盖它的值。通过使用 5.2.20 覆盖它,您将 spring-framework-bom 5.2.8 替换为 5.2.20,这有效地将大部分 spring 包拉到 5.2.20