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

Provide Spring Boot git and build information via /actuator/info endpoint when using maven as a build tool

我正在使用这个 Spring 引导指南 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>

例如,您可以将以下内容添加到 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

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

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 Information and Generate 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 Information and Generate Build Information

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

首先,添加插件(不需要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 -> compile。现在,在 target/classes 中应该会生成 git.properties 和带有 build-info.properties.

的 META-INF 文件夹

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

下面是 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