使用 maven 作为构建工具时,通过 /actuator/info 端点提供 Spring Boot git 和构建信息

Mic*_*l S 4 spring-boot spring-boot-actuator

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

执行器api 文档提到了包含构建信息(如工件、组、名称、版本)和 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>
Run Code Online (Sandbox Code Playgroud)

San*_*ani 11

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

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

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
Run Code Online (Sandbox Code Playgroud)

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

management:
  endpoints:
    web:
      exposure:
        include: "health,info"
Run Code Online (Sandbox Code Playgroud)

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

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

springBoot {
    buildInfo()
}
Run Code Online (Sandbox Code Playgroud)

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

{"build":{"artifact":"actuator-service","name":"","time":"2022-01-12T18:16:28.468Z","version":"0.0.1-SNAPSHOT","group":"com.example"}}
Run Code Online (Sandbox Code Playgroud)

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

id "com.gorylenko.gradle-git-properties" version "1.5.1"
Run Code Online (Sandbox Code Playgroud)

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

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

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

management:
  info:
    git:
      enabled: true
      mode: full
Run Code Online (Sandbox Code Playgroud)

参考文献: 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


Mic*_*l S 8

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

Git 信息

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

<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
</plugin>
Run Code Online (Sandbox Code Playgroud)

查看Git 提交信息生成 Git 信息

构建信息

向 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>
Run Code Online (Sandbox Code Playgroud)

来源:构建信息生成构建信息

  • 如果您使用的是 Java 11,则需要使用版本 5.xx,如 https://github.com/git-commit-id/git-commit-id-maven-plugin#relocation-of-the- 中所述项目。请注意分别更新的 groupId 和 artifactId、io.github.git-commit-id 和 git-commit-id-maven-plugin。 (3认同)