【问题标题】:How to get the version of Spring Boot in the /info actuator endpoint如何在 /info 执行器端点中获取 Spring Boot 的版本
【发布时间】:2017-09-18 13:55:18
【问题描述】:

可以通过添加${spring-boot.version}在banner.txt中包含Spring Boot的版本。

如何对/info 执行器端点执行相同操作? 我尝试将以下两项添加到我的应用程序属性中,但没有成功:

  • info.app.spring-boot.version=${spring-boot.version}
  • info.app.spring-boot.version=@spring-boot.version@

/info 端点将打印“${spring-boot.version}”(或“@spring-boot.version@”),不解析占位符变量

我的下一个想法是为 Spring Boot 版本创建一个 Maven 属性,并像这样在父部分中引用它

<properties>
    <spring.boot.version>1.5.3.RELEASE</spring.boot.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>${spring.boot.version}</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

但这不起作用,因为 Maven 在处理 &lt;parent&gt; 部分之后解析占位符变量

更新

以下方法确实有效,但并不理想:

@Component
public class MyInfoContributor implements InfoContributor {
    @Override
    public void contribute(Info.Builder builder) {

        builder.withDetail("spring-boot.version", SpringBootVersion.getVersion());
    }
}

【问题讨论】:

  • 为什么您的更新解决方案不理想?它准确地返回你想要的......否则配置maven来处理你的属性文件以替换@prop@。不过我可能会选择InfoContributor

标签: java spring-boot spring-boot-actuator


【解决方案1】:

这应该可行:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <additionalProperties>
      <spring-boot-version>${spring.boot.version}</spring-boot-version>
    </additionalProperties>
  </configuration>
</plugin>

然后你就不需要 app.info 属性了。

【讨论】:

    【解决方案2】:

    正如您在更新中提到的,InfoContributor 可用于根据 SpringBootVersion 中的值添加此属性:

    @Component
    public class SpringBootVersionInfoContributor implements InfoContributor {
        @Override
        public void contribute(Info.Builder builder) {
            builder.withDetail("spring-boot.version", SpringBootVersion.getVersion());
        }
    }
    

    这似乎是添加此细节的一种非常可靠的惯用方法。

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 2018-06-18
      • 2020-05-25
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2016-08-26
      • 1970-01-01
      相关资源
      最近更新 更多