How to display maven, git and building related information on the Springboot Actuator/info node

Table of contents

1. Prepare a spring-boot project and expose Actuator/info nodes

2. Display Maven-related values

3. Display git related values

4. Add build info


1. Prepare a spring-boot project and expose Actuator/info nodes

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. Display Maven-related values

In the application.properties file, add the following key-value attributes

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

If you are not using the starter parent, you need to include the following element in the <build/> element of pom.xml

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

After executing the maven clean compile -DskipTests command, you will find that the maven-related placeholders defined in application.properties have been replaced.

The result is as follows:

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

Start the spring-boot project, visit the actuator/info node, and you can see the following information.

{
  "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. Display git related values

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

After executing maven clean install-DskipTests, you will find that a git.properties file is generated under your classes directory.

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

Start the spring-boot project, visit the actuator/info node, and you can see the following information.

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

Display maven and git information together

{
  "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. Add build info

Adding useful build information helps to quickly identify build artifact name, version, creation time, etc. It can come in handy to check if the team has deployed the relevant version of the application. Spring Boot allows adding this easily using Maven or Gradle build plugins.

<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>

After compiling the code, the target/classes/META-INF/build-info.properties file will be generated

The content is as follows

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

Custom properties can also be added to this list using the additionalProperties attribute:

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

If we now run the application and open the http://localhost:8080/actuator/info endpoint in a browser, we will see a response similar to the following:

{
  "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"
  }
}

If you want to exclude any properties you may configure using excludeInfoProperties. Let's see how to exclude artifact properties:

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

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

Related source code classes in SpringBoot

org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration

Exposing a Helpful Info Endpoint with Spring Boot Actuator

Guess you like

Origin blog.csdn.net/keeppractice/article/details/130067119