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

6

12

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>
Regina answered 1/1, 2020 at 21:55 Comment(0)
R
15

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

Regina answered 2/1, 2020 at 19:22 Comment(2)
If you're on Java 11 you'll need to use version 5.x.x as described in github.com/git-commit-id/…. Note the updated groupId and artifactId, io.github.git-commit-id and git-commit-id-maven-plugin respectively.Hoedown
@Hoedown are you sure you need 5.x on java11? I'm using it and don't see problems, and can't find anything hinting at that restriction. Did you mean that if you're on java8, you can't use 5.x (because 5.x requires java11, but 4.x also just works on java11) ?Regal
M
12

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

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

Muck answered 12/1, 2022 at 18:33 Comment(1)
Thanks, exactly what I need. Though management.info.git.mode=simple is enough for meEnterovirus
V
2

You can do that for example by adding the following to your application.properties

[email protected]@
[email protected]@
[email protected]@
[email protected]@

Source: https://dzone.com/articles/magic-with-spring-boot-actuator

View answered 1/1, 2020 at 22:27 Comment(0)
P
2

I had the same problem, /actuator/info always returns {}

First, add plugins (lombok is not necessary):

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>build-info</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
</plugin>

Second, go Maven -> compile. Now, in target/classes should be generated git.properties and META-INF folder with build-info.properties.

Finally, run your app and that's it!

Polemoniaceous answered 2/9, 2021 at 2:41 Comment(0)
B
1

Something was missing for all the above responses so here is how I made it:

<plugin>
  <groupId>pl.project13.maven</groupId>
  <artifactId>git-commit-id-plugin</artifactId>
  <version>4.9.10</version>
  <configuration>
                <failOnNoGitDirectory>false</failOnNoGitDirectory>
                <!-- this is false by default, forces the plugin to generate the git.properties file -->
                <generateGitPropertiesFile>true</generateGitPropertiesFile>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
  </configuration>
</plugin>

and

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

Then the git.properties file will be generated, so then you can basically check the key of the value you want and access it such as:

@git.build.version@
Bracci answered 25/8, 2023 at 8:57 Comment(0)
E
1

Please check the official guide here: https://github.com/git-commit-id/git-commit-id-maven-plugin/blob/master/docs/using-the-plugin.md

          <plugin>
            <groupId>io.github.git-commit-id</groupId>
            <artifactId>git-commit-id-maven-plugin</artifactId>
            <version>5.0.0</version>
            <executions>
                <execution>
                    <id>get-the-git-infos</id>
                    <goals>
                        <goal>revision</goal>
                    </goals>
                    <phase>initialize</phase>
                </execution>
            </executions>
            <configuration>
                <generateGitPropertiesFile>true</generateGitPropertiesFile>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                <includeOnlyProperties>
                    <includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
                    <includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
                </includeOnlyProperties>
                <commitIdGenerationMode>full</commitIdGenerationMode>
            </configuration>
        </plugin>
Excellency answered 8/12, 2023 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.