【问题标题】:Provide Spring Boot git and build information via /actuator/info endpoint when using maven as a build tool使用 maven 作为构建工具时,通过 /actuator/info 端点提供 Spring Boot git 和构建信息
【发布时间】:2020-04-20 17:01:13
【问题描述】:

我正在使用这个 Spring Boot 指南 Building a RESTful Web Service with Spring Boot Actuator。访问端点 /actuator/info 时,我得到空的 json 响应 {}

执行器api documentation 提到了响应结构,其中包含工件、组、名称、版本等构建信息和分支、提交等git信息。

如何启用记录在案的响应结构。我想使用 maven 作为构建工具(不是 gradle)。这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>actuator-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>actuator-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

    标签: spring-boot spring-boot-actuator


    【解决方案1】:

    例如,您可以通过将以下内容添加到 application.properties 来做到这一点

    info.app.name=@project.name@
    info.app.version=@project.version@
    info.app.encoding=@project.build.sourceEncoding@
    info.app.java.version=@java.version@
    

    来源:https://dzone.com/articles/magic-with-spring-boot-actuator

    【讨论】:

      【解决方案2】:

      经过进一步研究,我在文档中找到了答案:

      Git 信息

      将此添加到 pom.xml 的插件部分。 maven 将在构建./target/classes/git.properties 期间生成此文件。 Spring 将读取该文件的内容并将其包含在/actuator/info 的响应中

      <plugin>
          <groupId>pl.project13.maven</groupId>
          <artifactId>git-commit-id-plugin</artifactId>
      </plugin>
      

      Git Commit InformationGenerate Git Information

      构建信息

      向 spring-boot-maven 插件添加执行目标。这将生成文件./target/classes/META-INF/build-info.properties。 Spring 将读取该文件的内容并将其包含在 /actuator/info 的响应中

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

      来源:Build InformationGenerate Build Information

      【讨论】:

      • 如果您使用的是 Java 11,则需要使用 github.com/git-commit-id/… 中所述的 5.x.x 版本。注意更新后的 groupId 和 artifactId,分别是 io.github.git-commit-id 和 git-commit-id-maven-plugin。
      【解决方案3】:

      我遇到了同样的问题,/actuator/info 总是返回 {}

      首先,添加插件(不需要lombok):

      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
              <excludes>
                  <exclude>
                      <groupId>org.projectlombok</groupId>
                      <artifactId>lombok</artifactId>
                  </exclude>
              </excludes>
          </configuration>
          <executions>
              <execution>
                  <goals>
                      <goal>build-info</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>
      <plugin>
          <groupId>pl.project13.maven</groupId>
          <artifactId>git-commit-id-plugin</artifactId>
      </plugin>
      

      其次,转到Maven -&gt; compile。现在,应该在target/classes 中生成带有 build-info.properties 的 git.properties 和 META-INF 文件夹。

      最后,运行您的应用程序,就是这样!

      【讨论】:

        【解决方案4】:

        以下是 Gradle 上的工作解决方案。 7.3.2 版 SpringBoot 版本:2.6.1

        包括项目的执行器。应将以下依赖项添加到 build.gradle 文件中。

        dependencies {
            implementation 'org.springframework.boot:spring-boot-starter-actuator'
        }
        

        默认情况下,只有健康状况可通过网络获得。因此,要启用信息执行器,请在 application.yml 中添加以下条目

        management:
          endpoints:
            web:
              exposure:
                include: "health,info"
        

        现在,当我们运行应用程序并尝试访问 /actuator/info 端点时,它会打印空 json 作为响应。这是信息执行器端点的默认行为。

        要从 build.gradle 生成 buildInfo,请在您的 gradle 文件中添加以下内容

        springBoot {
            buildInfo()
        }
        

        现在,如果您运行应用程序并点击 /actuator/info 端点,输出将是您项目的构建信息

        {"build":{"artifact":"actuator-service","name":"","time":"2022-01-12T18:16:28.468Z","version":"0.0.1-SNAPSHOT","group":"com.example"}}
        

        另外我们可以配置生成 git 提交信息。为此,您必须申请以下插件

        id "com.gorylenko.gradle-git-properties" version "1.5.1"
        

        完成后,在项目构建中,它将在您的 build/resources 文件夹中生成一个名为 git.properties 的文件。

        现在 /actuator/info 端点也将从 git.properties 生成 git 信息。默认情况下,它不会从 git.properties 生成所有配置。

        如果您想在 /info 端点中查看完整的 git 配置,请在 application.yml 中执行以下配置

         info:
            git:
              enabled: true
              mode: full
        

        参考资料: https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/howto-build.html#howto-build-info https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/howto-build.html#howto-git-info

        【讨论】:

          猜你喜欢
          • 2019-01-19
          • 2016-12-20
          • 2018-03-14
          • 2016-03-27
          • 2018-08-15
          • 2021-08-25
          • 2016-02-10
          • 2019-01-23
          • 2018-03-03
          相关资源
          最近更新 更多