spring-boot - 使用 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>
解决方案
经过进一步研究,我在文档中找到了答案:
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>
构建信息
向 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>
推荐阅读
- c# - 带有子实体的 EFUtilities 批量 InsertAll
- java - org.glassfish.jersey.process.internal.RequestScoped 有多个活动上下文
- sql - 密集排名,按 A 列分区,按 B 列更改递增,但按 C 列排序
- php - PHP 如何正确创建 url?
- c# - 如何从 C# 中的接收 CoAP 包中获取 IP 地址?
- c++ - 为什么“(!v.empty())”比“(v.size()> 0)”更好?
- php - (内部)加入此表时导致此内存泄漏的原因是什么?
- regex - 正则表达式向后读取到文件路径中的句号
- clojure - 替代在 Clojure 中使用 REPL 来快速尝试东西
- android - 从 FirebaseMessagingService 调用 Activity 方法