【问题标题】:Display Build time in spring boot /info endpoint在 spring boot /info 端点中显示构建时间
【发布时间】:2018-03-14 22:22:09
【问题描述】:

我在我的 spring boot application.yml 中配置了以下属性

info:
  app:
    name: @project.artifactId@
    description: @project.description@
    version: @project.version@
    timestamp: @timestamp@

添加Spring Boot Actuator依赖后,我可以访问/info端点并查看信息。

为了显示时间戳信息,我在maven项目的pom.xml中添加如下属性,如下所示,

<properties>
   <timestamp>${maven.build.timestamp}</timestamp>
    <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
</properties>

时间戳以正确的格式显示,但不正确。我的意思是我在 IST 时区,值显示为, 时间戳:“2017-10-03T16:24:02Z”这是不正确的,可能它以 GMT 时间格式显示。但是,我想要 IST 格式。

有人可以帮我解决这个问题吗?

【问题讨论】:

标签: maven spring-boot spring-boot-actuator spring-boot-maven-plugin


【解决方案1】:

默认情况下,Maven 发出 UTC 格式的 maven.build.timestamp

您可以使用Maven Build Helper Plugintimestamp-property 目标在不同的时区发出时间戳。

这是一个例子:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>timestamp-property</id>
            <goals>
                <goal>timestamp-property</goal>
            </goals>
            <configuration>
                <name>build.timestamp.with.offset</name>
                <pattern>yyyy-MM-dd'T'HH:mm:ss'Z'</pattern>
                <timeZone>IST</timeZone>
            </configuration>
        </execution>
    </executions>
</plugin>

我刚刚使用该插件和您的问题中定义的属性运行了一个构建,我正在回显timestamp 属性和build.timestamp.with.offset 属性的值:

[INFO] Executing tasks
     [echo] [timestamp]: 2017-10-04T08:14:58Z
     [echo] [build.timestamp.with.offset]: 2017-10-04T12:44:59Z

这清楚地表明默认时间戳采用 UTC,build.timestamp.with.offset 采用 IST。

因此,您可以使用此插件,然后更新您的 application.yaml 以使用 build.timestamp.with.offset 属性。

【讨论】:

  • 谢谢。有没有办法自动检测时区并以当前时区格式生成数据。
  • @all4u:我不知道。查看code,除非您提供值,否则它似乎默认为 GMT。您可以fork the plugin 并更改timestamp-property 的行为以满足您的要求。
猜你喜欢
  • 2018-03-03
  • 2019-01-19
  • 2018-08-15
  • 2016-03-27
  • 2020-02-07
  • 2016-12-20
  • 2021-08-25
  • 2020-04-20
  • 1970-01-01
相关资源
最近更新 更多