spring boot-获得Maven-pom中版本号

文章讲述了如何在SpringBoot应用中,通过pom.xml文件获取项目版本号,并将版本号写入application.yml,然后使用@Value注解在控制器中注入版本号,以便在运行时能通过API获取到当前应用的版本信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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.2.RELEASE</version>
        <relativePath/>
    </parent>

    <artifactId>demo-version</artifactId>
    <version>1.0.0</version>
    <description>获取pom的版本</description>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

修改application.yml配置

编译构建后,application.yml真实内容(注意version变成了真实值):

project:
  version: @project.version@

【注意:是@@,不是${}

测试

@RestController
public class TestController {
    @Value("${project.version}")
    public String version;

    @GetMapping("version")
    public String getVersion() {
        return version;
    }
}

通过 @Value 注入获取配置信息

浏览输入: http://localhost:8080/version
在这里插入图片描述
成功获取到版本号

### spring-boot-maven-plugin 支持的版本列表 `spring-boot-maven-plugin` 是 Spring Boot 提供的一个 Maven 插件,主要用于支持 Spring Boot 项目的构建和打包操作。该插件的版本通常与对应的 Spring Boot 版本保持一致,因此其支持的版本列表可以依据官方发布的 Spring Boot 版本来推断。 以下是 `spring-boot-maven-plugin` 的一些常见版本及其对应关系: #### 官方支持的主要版本 | **Spring Boot Version** | **Maven Plugin Version** | |-------------------------|--------------------------| | 3.x | 3.x | | 2.7.x | 2.7.x | | 2.6.x | 2.6.x | | 2.5.x | 2.5.x | | 2.4.x | 2.4.x | | 2.3.x | 2.3.x | 这些版本的具体功能和支持特性可以通过查看 [Spring Boot Release Notes](https://github.com/spring-projects/spring-boot/releases) 获取更多信息[^1]。 #### 如何确认当前使用的版本? 当在项目中使用 `spring-boot-starter-parent` 作为父 POM 时,`spring-boot-maven-plugin` 的版本会被自动继承并设置为与 Spring Boot 版本相同。例如,在以下配置中: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.9</version> </parent> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` 这里无需显式声明插件版本,因为它会默认采用父 POM 中定义的 Spring Boot 版本号[^2]。 #### 如果遇到版本冲突怎么办? 如果手动指定了插件版本(如 `<version>`),可能会因为不兼容而导致编译失败。例如,某些旧版 JDK 可能无法加载由较新 Java 编译器生成的类文件。这种情况下需要升级 JDK 或调整插件版本以匹配环境需求[^4]。 #### 示例:如何解决版本问题 假设项目运行于 JDK 8 上,而尝试使用了 `spring-boot-maven-plugin:3.0.0`,则可能触发如下错误: ``` Unable to load the mojo 'build-info' in the plugin 'org.springframework.boot:spring-boot-maven-plugin:3.0.0' due to an API incompatibility... ``` 解决方案之一是降级到适合 JDK 8 的插件版本,比如 `2.7.x` 系列。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值