在Springboot Actuator/info 节点上怎么显示maven,git 和building相关的信息

目录

1. 准备一个spring-boot 的项目,并且暴露Actuator/info的节点

2. 显示Maven相关的值

3. 显示git 相关的值

4. 加build info


1. 准备一个spring-boot 的项目,并且暴露Actuator/info的节点

pom.xml


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

java code

@SpringBootApplication()
public class TestApp {

    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
    }
}

application.properties

management.info.env.enabled=true

2. 显示Maven相关的值

在application.properties文件里,加入下面key-value属性

[email protected]@
[email protected]@
[email protected]@
[email protected]@
[email protected]@
[email protected]@
management.info.env.enabled=true

如果您不使用starter parent,则需要在pom.xml的<build/>元素中包含以下元素

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

执行maven clean compile -DskipTests命令后,你会发现application.properties里定义的maven相关的占位符已经被替换了。

结果如下:

info.app.name=test_project
info.app.description=Parent pom providing dependency and plugin management for applications built with Maven
info.app.version=1.0-SNAPSHOT
info.app.encoding=UTF-8
info.app.java.source=1.8.0_321
info.app.java.target=1.8.0_321

启动spring-boot的项目,访问actuator/info节点,就可以看到下面的信息了。

{
  "app" : {
    "name" : "test_project",
    "description" : "Parent pom providing dependency and plugin management for applications built with Maven",
    "version" : "1.0-SNAPSHOT",
    "encoding" : "UTF-8",
    "java" : {
      "source" : "1.8.0_321",
      "target" : "1.8.0_321"
    }
  },
}

3. 显示git 相关的值

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

执行maven clean install-DskipTests 后,你会发现在你的classes目录下面有个git.properties文件生成。

git.branch=dev
git.commit.id=xxx
git.commit.user.name=xxx
......

启动spring-boot的项目,访问actuator/info节点,就可以看到下面的信息了。

{
  "git" : {
    "commit" : {
      "time" : "2023-04-06T12:16:17Z",
      "id" : "e3dd1233"
    },
    "branch" : "dev"
  }
}

一起显示maven和git 的信息

{
  "app" : {
    "name" : "test_project",
    "description" : "Parent pom providing dependency and plugin management for applications built with Maven",
    "version" : "1.0-SNAPSHOT",
    "encoding" : "UTF-8",
    "java" : {
      "source" : "1.8.0_321",
      "target" : "1.8.0_321"
    }
  },
  "git" : {
    "commit" : {
      "time" : "2023-04-06T12:16:17Z",
      "id" : "e3dd1233"
    },
    "branch" : "dev"
  }
}

4. 加build info

添加有用的构建信息有助于快速识别构建工件名称、版本、创建时间等。它可以派上用场来检查团队是否部署了应用程序的相关版本。 Spring Boot 允许使用 Maven 或 Gradle 构建插件轻松添加它。

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

编译完代码后会生成target/classes/META-INF/build-info.properties文件

内容如下

build.artifact=spring-boot-build-info
build.group=io.reflectoring
build.name=spring-boot-build-info
build.time=2022-03-06T05\:53\:45.236Z
build.version=0.0.1-SNAPSHOT

还可以使用 additionalProperties 属性将自定义属性添加到此列表:

<execution>
  <goals>
    <goal>build-info</goal>
  </goals>
  <configuration>
    <additionalProperties>
      <custom.key1>value1</custom.key1>
      <custom.key2>value2</custom.key2>
    </additionalProperties>
  </configuration>
</execution>

如果我们现在运行应用程序并在浏览器中打开 http://localhost:8080/actuator/info 端点,我们将看到类似于以下的响应:

{
  "build": {
    "custom": {
      "key2": "value2",
      "key1": "value1"
    },
    "version": "0.0.1-SNAPSHOT",
    "artifact": "spring-boot-build-info",
    "name": "spring-boot-build-info",
    "time": "2022-03-06T06:34:30.306Z",
    "group": "io.reflectoring"
  }
}

如果要排除任何可能使用 excludeInfoProperties 配置的属性。让我们看看如何排除工件属性:

<configuration>
  <excludeInfoProperties>
    <infoProperty>artifact</infoProperty>
  </excludeInfoProperties>
</configuration>

Please refer to the official Spring Boot ​​​​​​documentation to know more.

SpringBoot中相关的源码类

org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration

Exposing a Helpful Info Endpoint with Spring Boot Actuator

猜你喜欢

转载自blog.csdn.net/keeppractice/article/details/130067119