Friday 20 September 2019

Spring boot Actuator: Add build details to info endpoint


It is very simple, just add 'build-info' goal to spring-boot-maven-plugin.
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>build-info</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Find the below working application.


Step 1: Create new maven project ‘actuatorDemo’. Project structure looks like below.


Step 2: Update pom.xml with maven dependencies.
<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-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
 </dependency>

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

</dependencies>


pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>actuatorDemo</groupId>
  <artifactId>actuatorDemo</artifactId>
  <version>1</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath />
  </parent>

  <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-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>

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

  </dependencies>

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


</project>

Step 3: Create a package ‘com.sample.app’ and define App.java like below.


App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Step 4: Create a package ‘com.sample.app.controller’ and define HelloController like below.

HelloController.java
package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

 @RequestMapping("/")
 public String home() {
  return "Welcome to spring boot developement";
 }

}

Step 5: Create application.properties file under src/main/resources folder.


application.properties
spring.security.user.name=krishna
spring.security.user.password=password123
spring.security.user.roles = ["USER", "ADMIN"]

info.team.name=Alaska
info.team.size=23
info.team.dev=krishna, ram, Sunil, Bikash
info.team.test=hari, pani, panth

info.version=1.23
info.product.owner=Ashwani
info.product.architect=Surav

Total project structure looks like below.

Run App.java.


Open the url 'http://localhost:8080/actuator/info',  (it will redirect you to login page, enter user name as krishna and password as password123) you will get below response.
{
  "team": {
    "name": "Alaska",
    "size": "23",
    "dev": "krishna, ram, Sunil, Bikash",
    "test": "hari, pani, panth"
  },
  "version": "1.23",
  "product": {
    "owner": "Ashwani",
    "architect": "Surav"
  },
  "build": {
    "version": "1",
    "artifact": "actuatorDemo",
    "name": "actuatorDemo",
    "group": "actuatorDemo",
    "time": "2019-09-05T05:49:32.599Z"
  }
}

As you see the response, it contains build information.

You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment