首页 > 解决方案 > 使用 maven 作为构建工具时,通过 /actuator/info 端点提供 Spring Boot git 和构建信息

问题描述

我正在使用这个 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>

标签: spring-bootspring-boot-actuator

解决方案


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

Git 信息

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

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

请参阅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>

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


推荐阅读