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

相关推荐
非 白5 分钟前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
吃海鲜的骆驼10 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
m0_7482336412 分钟前
RabbitMQ 进阶
android·前端·后端
羱滒14 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
m0_7482386321 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
桦说编程23 分钟前
【硬核总结】如何轻松实现只计算一次、惰性求值?良性竞争条件的广泛使用可能超过你的想象!String实际上是可变的?
后端·函数式编程
Forget the Dream42 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek1 小时前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学