To start your application using a different Spring Boot version

To start your application using a different Spring Boot version by configuring the pom.xml, follow these steps:


Configuring pom.xml for Runtime Spring Boot Version Switching

  1. Set the Spring Boot Version as a Maven Property: Define the Spring Boot version using a property in the pom.xml, allowing it to be overridden at runtime.

    <properties> <spring.boot.version>2.5.15</spring.boot.version> <!-- Default Version --> </properties>

  2. Use dependencyManagement for Spring Boot Dependencies: Import the Spring Boot BOM (Bill of Materials) using the defined version property. This ensures all Spring Boot dependencies use the specified version.

    <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

  3. Add Spring Boot Dependencies as Usual: Use Spring Boot dependencies without specifying their version, as the BOM ensures they inherit the correct version.

    复制代码

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

  4. Override the Version at Build or Runtime: You can specify a different Spring Boot version by overriding the property when building or running the application.

    Build Time:

    mvn clean package -Dspring.boot.version=2.3.12.RELEASE

    Runtime (with Executable JAR): If you have already built the application but want to run it with a different version:

    • Ensure the dependencies are not embedded in the JAR (spring-boot-maven-plugin configuration).
    • Provide the required version in the classpath at runtime.

    java -Dspring.boot.version=2.3.12.RELEASE -jar target/your-app.jar


Example pom.xml for Dynamic Version Switching

Here is a complete example of a pom.xml that supports switching Spring Boot versions:

<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> <groupId>com.example</groupId> <artifactId>spring-boot-version-switch</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <!-- Define the Spring Boot version property --> <spring.boot.version>2.5.15</spring.boot.version> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <!-- Import Spring Boot dependencies dynamically --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Add your Spring Boot dependencies without specifying versions --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> </build> </project>


How to Test Different Versions

  • Default Version:

    mvn clean package

  • Different Version (e.g., 2.3.12.RELEASE):

    mvn clean package -Dspring.boot.version=2.3.12.RELEASE

The dependencies will automatically switch to the specified version

相关推荐
ByteBlossom666几秒前
Clojure语言的正则表达式
开发语言·后端·golang
代码驿站5205 分钟前
C语言的正则表达式
开发语言·后端·golang
n北斗6 分钟前
arr.length 和 string.length()
java
云端 架构师8 分钟前
PL/SQL语言的正则表达式
开发语言·后端·golang
Linux运维老纪26 分钟前
Go语言之十条命令(The Ten Commands of Go Language)
服务器·开发语言·后端·golang·云计算·运维开发
野生派蒙28 分钟前
排序算法:冒泡排序
java·数据结构·算法·排序算法
小万编程31 分钟前
【2025最新计算机毕业设计】基于SpringBoot+Vue教研听课管理系统(高质量源码,提供文档,免费部署到本地)
java·spring boot·计算机毕业设计·java毕业设计·java毕设源码·web毕业设计
JINGWHALE139 分钟前
设计模式 行为型 命令模式(Command Pattern)与 常见技术框架应用 解析
前端·人工智能·后端·设计模式·性能优化·系统架构·命令模式
啾啾Fun40 分钟前
[java基础]LinkedList源码粗析
java·开发语言
白露与泡影41 分钟前
2025 年春招互联网大厂226 道 Java 高级岗面试题
java·开发语言