Michael S
Michael S

Reputation: 880

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>

Upvotes: 12

Views: 25945

Answers (6)

zhyd1997
zhyd1997

Reputation: 175

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>

Upvotes: 1

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4839

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

Upvotes: 13

Cublax
Cublax

Reputation: 1409

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@

Upvotes: 1

Askelov
Askelov

Reputation: 21

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!

Upvotes: 2

Michael S
Michael S

Reputation: 880

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

Upvotes: 16

laserany
laserany

Reputation: 1451

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

Upvotes: 2

Related Questions