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

相关推荐
智慧物业老杨7 分钟前
智慧物业数智化转型实战:从工单响应到业主满意度的闭环构建
java·开发语言
Kiling_07049 分钟前
Java集合框架:List集合详解与应用
java·开发语言·windows
DeepNoMind20 分钟前
从入门到 Offer:系统设计面试的完整备考路线
后端
极客先躯26 分钟前
高级java每日一道面试题-2025年12月08日-实战篇[Docker]-如何为 Docker 配置代理?如何为容器配置代理?
java·docker·代理配置的双层架构·docker 守护进程配置代理·为容器配置代理·构建时环境变量·运行时注入环境变量
xun-ming29 分钟前
SpringBoot和Vue3实战阿里百炼大模型极简版
spring boot·ai·vue3·智能体·百炼大模型
csdn2015_33 分钟前
java springboot 文件导入,判断第一列的值是否有重复
java·windows·spring boot
~|Bernard|1 小时前
四,go语言中GMP调度模型
java·前端·golang
Tisfy1 小时前
LeetCode 2553.分割数组中数字的数位:模拟(maybe+翻转)——java也O(1)
java·数学·算法·leetcode·题解·模拟·取模
阿丰资源1 小时前
基于Springboot+mysql的在线兼职平台(附源码)
spring boot·后端·mysql
怪祝浙1 小时前
从简单项目入手Java(学生系统)V6(Web版本 Spring Boot3 MySQL Vue3 MyBatis)
java·spring boot·mysql