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

如何配置spring-boot-maven-plugin使actuator输出ISO格式构建时间

问题说明
  • 需求:在Spring Boot Actuator的info端点中,以ISO 8601格式(格式为yyyy-MM-dd'T'HH:mm:ssZ)展示构建时间,构建信息由spring-boot-maven-plugin生成。
  • 项目背景:未使用Spring Boot官方父子工程结构,采用自定义父POM,大部分配置为手动设置,存在配置遗漏可能。
  • 当前现象:访问localhost:9090/actuator/info时,返回的build信息中time字段为epoch时间戳格式,返回示例如下:
"build":{
  "artifact":"application",
  "name":"application",
  "time":1657089305.677000000,
  "version":"0.1.0-SNAPSHOT",
  "group":"my.group"
 }
现有配置

application.yml配置

management:
  server:
    port: 9090
  endpoints:
    web:
      exposure:
        include: health,info
  info:
    build:
      enabled: true
    java:
      enabled: true
    git:
      enabled: true

pom.xml中spring-boot-maven-plugin配置

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

已尝试的无效配置

曾在Maven属性中配置maven.build.timestamp.format参数,未生效,配置如下:

<properties>
    <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
</properties>
问题原因

配置的maven.build.timestamp.format是Maven内置构建时间戳的格式参数,和spring-boot-maven-plugin生成build-info.properties时的时间序列化逻辑无关。
Spring Boot 2.x版本中,build-info goal默认将构建时间以Instant类型写入properties文件,Actuator序列化该值时默认输出为epoch时间戳格式,不会自动转成ISO 8601字符串。插件文档提到的outputTimestamp参数是用于可复现构建时统一归档条目时间的,不会影响build-info中time字段的生成格式,不需要额外配置该参数。

解决方案

两种可行方案按需选择即可:

方案1:自定义时间序列化规则(无侵入,无需修改构建配置)

在项目中添加Jackson序列化配置,直接将BuildProperties的time字段格式化为目标ISO格式:
新增Jackson配置类,自定义Instant类型在Actuator响应中的序列化格式:

import com.fasterxml.jackson.datatype.jsr310.ser.InstantSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.format.DateTimeFormatterBuilder;

@Configuration
public class JacksonConfig {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer actuatorJsonCustomizer() {
        return builder -> builder.serializers(new InstantSerializer(
                InstantSerializer.INSTANCE,
                false,
                new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd'T'HH:mm:ssZ").toFormatter()
        ));
    }
}

注意:如果项目其他接口也用到Instant类型序列化且需要不同格式,不要用全局配置,改用方案2。

方案2:在build-info插件中显式指定格式化后的构建时间

直接在spring-boot-maven-plugin的build-info goal配置中,添加自定义的time属性,值为格式化后的Maven时间戳,让生成的properties文件中直接存储格式化后的字符串:
修改pom.xml中插件配置,显式传入构建时间属性:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>2.6.3</version>
  <executions>
    <execution>
      <goals>
        <goal>build-info</goal>
        <goal>repackage</goal>
      </goals>
      <configuration>
        <additionalProperties>
          <build.time>${maven.build.timestamp}</build.time>
        </additionalProperties>
      </configuration>
    </execution>
  </executions>
</plugin>

保留之前配置的maven.build.timestamp.format属性即可,此时插件生成的build-info.properties中build.time就是格式化后的字符串,Actuator会直接输出该字符串值,不会转成时间戳。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 17:21:26