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

相关推荐
天天摸鱼的java工程师2 分钟前
RocketMQ 与 Kafka 对比:消息队列选型的核心考量因素
java·后端
星浩AI4 分钟前
10 行代码带你上手 LangChain 智能 Agent
人工智能·后端
uup4 分钟前
SpringBoot 集成 Redis 分布式锁实战:从手动实现到注解式优雅落地
java·redis
Java陈序员6 分钟前
数据同步神器!一款搞定多种数据源同步的开源中间件!
java·spring boot·mysql
洛卡卡了8 分钟前
从活动编排到积分系统:事件驱动在业务系统中的一次延伸
前端·后端·面试
superman超哥11 分钟前
Rust 堆内存与栈内存的所有权管理:精确控制的内存模型
开发语言·后端·rust·编程语言·内存模型·堆内存与栈内存·所有权管理
用户83071968408212 分钟前
Spring Boot 核心事件全解析:启动链路 + 监听器作用详解
spring boot
liuhaikang20 分钟前
鸿蒙高性能动画库——lottie-turbo
java·开发语言·nginx
面对疾风叭!哈撒给24 分钟前
Liunx之Docker 安装启动 influxdb2
java·spring cloud·docker
沛沛老爹26 分钟前
Web开发者快速上手AI Agent:基于Function Calling的提示词应用优化实战
java·人工智能·llm·agent·web·企业开发·function