Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using this Spring Boot guide Building a RESTful Web Service with Spring Boot Actuator. When accessing endpoint /actuator/info I am getting empty json response {}.

The actuator api documentation mentions response structures which contain build information like artifact, group, name, version and git information like branch, commit etc.

How can I enable the documented response structures. I want to use maven as build tool (not gradle). This is my 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>
like image 664
Michael S Avatar asked Jan 01 '20 21:01

Michael S


2 Answers

After further research I found the answer in the documentation:

Git Information

Add this to plugins section of pom.xml. maven will generate this file during build ./target/classes/git.properties. Spring will read contents of this file and include it in the response of /actuator/info

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

See Git Commit Information and Generate Git Information

Build Information

Add an execution goal to spring-boot-maven plugin. This will generate the file ./target/classes/META-INF/build-info.properties. Spring will read contents of this file and include it in the response of /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>

Source: Build Information and Generate Build Information

like image 127
Michael S Avatar answered Sep 18 '22 16:09

Michael S


Below is the working solution on Gradle. Gralde Version 7.3.2 SpringBoot Version: 2.6.1

To include actuators for the project. below dependency should be added to the build.gradle file.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
}

By default only health is available over web. So to enable the info actuator add below entry in your application.yml

management:
  endpoints:
    web:
      exposure:
        include: "health,info"

Now when we run the application and attempt to access the /actuator/info end point it prints empty json in response. This is the default behavior of info actuator end point.

To generate the buildInfo from build.gradle, add below in your gradle file

springBoot {
    buildInfo()
}

Now if you run the application and hit /actuator/info endpoint, output will be your project's build info

{"build":{"artifact":"actuator-service","name":"","time":"2022-01-12T18:16:28.468Z","version":"0.0.1-SNAPSHOT","group":"com.example"}}

Additional we can configure to generate the git commit information. To do this, you have to apply below plugin

id "com.gorylenko.gradle-git-properties" version "1.5.1"

Once done, on the project build, it will generate a file called git.properties in your build/resources folder.

And now the /actuator/info endpoint will also generated the git information from the git.properties. By default it won't generate all configs from git.properties.

If you want to see full git configuration in /info endpoint, do the below config in application.yml

 info:
    git:
      enabled: true
      mode: full

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

like image 36
Sanjay Bharwani Avatar answered Sep 16 '22 16:09

Sanjay Bharwani