【问题标题】:Spring Boot Actuator Info Endpoint Version from Gradle FileGradle 文件中的 Spring Boot Actuator 信息端点版本
【发布时间】:2015-06-11 16:41:33
【问题描述】:

我希望我的 Spring Boot (1.2.4.RELEASE) 应用程序中的 /info 执行器端点返回 build.gradle 文件中指定的版本。

在我的build.gradle 文件中,我有这样一行:

version = '0.0.1-SNAPSHOT'

我正在使用 yaml 配置文件。现在我在application.yml中复制了这个版本:

info:
    build:
        version: 0.0.1-SNAPSHOT  

这可能吗?

【问题讨论】:

    标签: gradle spring-boot


    【解决方案1】:

    将以下内容添加到 build.gradleFile。不需要创建 InfoContributor 类。

    plugins {
    id 'org.springframework.boot' version '<version>'
    }
    
    springBoot {
    buildInfo()
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
        etc...
    }
    

    查询http://host:port/actuator/info,您会看到类似...

      {
      "build": {
        "artifact": "spring-example",
        "name": "spring-example",
        "time": "2020-01-30T09:47:33.551Z",
        "version": "0.0.1-SNAPSHOT",
        "group": "com.example"
      }
    }
    

    注意使用 2.2.4.RELEASE 测试

    【讨论】:

      【解决方案2】:

      启动文档中详细说明了这个确切的用例:

      http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info-automatic-expansion-gradle

      所以在你的 build.gradle 中这样做

      version = '0.0.1-SNAPSHOT'
      processResources {
          expand(project.properties)
      }
      

      你的应用程序.yml

      info:
        build:
          version: ${version}
      
      1. 确保避开所有 spring 占位符,以免与 Gradle 冲突。两者都使用 ${} 作为替换令牌。 ${key} 应该变成 \${key} 。这将影响 src/main/resources 中的任何内容。

      2. 清理/构建和部署您的战争/jar。不要从你的 IDE 运行它,否则占位符不会被替换。

      【讨论】:

      • 虽然它引用了一个project.properties 文件,所以我看不到与Gradle 应用程序版本的联系......你知道project.properties 是如何适应的吗?
      • 它不是指一个文件。它是 Gradle 项目对象。如果您的 yml 文件中有其他 spring ${} 占位符,请小心,如链接中所述。那些需要被转义。
      • 此解决方案显然不适用于 Spring Boot 2 应用程序:org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'info.build.version' to java.lang.Object... 由 java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in value \"${version}\" 引起
      • @jst 是的,这只适用于 Spring boot 1.x。最好有一个 Spring Boot 2.x 答案。
      猜你喜欢
      • 2018-04-10
      • 2016-06-01
      • 2015-01-31
      • 2020-09-15
      • 2016-09-07
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多