You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot项目:如何通过app.yml在actuator/info显示应用版本?

问题分析与解决方案

1. 移除pom.xml冗余依赖

你同时引入了spring-boot-actuator和spring-boot-starter-actuator,后者已包含前者的所有依赖内容,保留starter即可:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. 修复app.yml语法错误

你的yml配置存在引号使用错误,破坏了配置结构:

  • description的双引号未闭合,换行导致配置失效
  • version的单引号位置错误,混淆了键值对边界

修正后的基础配置:

# 端点核心配置
management:
  endpoint:
    info:
      enabled: true
  endpoints:
    web:
      exposure:
        include: info # 必须配置此项,将info端点暴露给web访问

# 自定义info内容
info:
  app:
    name: Spring Sample Application
    description: "spring application"
    version: '1.0.0'

3. 从pom.xml读取快照版本到/actuator/info

要实现自动读取pom中的版本号,有两种常用方案:

方案一:Maven资源过滤注入版本

  1. 在pom.xml的<build>节点下添加资源过滤配置,允许Maven打包时替换yml中的占位符:
<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>
  1. 修改app.yml,用@project.version@占位符替换硬编码版本:
info:
  app:
    name: Spring Sample Application
    description: "spring application"
    version: @project.version@

方案二:通过Spring Boot插件自动生成构建信息

在pom.xml中配置spring-boot-maven插件生成build-info.properties,Actuator会自动读取该文件内容到info端点:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>build-info</goal>
      </goals>
    </execution>
  </executions>
</plugin>

此方案无需修改info配置,启动后访问/actuator/info会自动包含build版本、时间等信息。

验证效果

启动项目后访问/actuator/info,会返回包含pom版本的JSON响应,示例:

{
  "app": {
    "name": "Spring Sample Application",
    "description": "spring application",
    "version": "1.0.0-SNAPSHOT"
  }
}

内容的提问来源于stack exchange,提问作者Lawell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 17:26:05