Steve
Steve

Reputation: 55635

Spring Boot Actuator Info Endpoint Version from Gradle File

I'd like the /info actuator endpoint from my Spring Boot (1.2.4.RELEASE) application to return the version as specified in the build.gradle file.

In my build.gradle file I have a line as so:

version = '0.0.1-SNAPSHOT'

I am using yaml configuration file. Right now I have the version duplicated as so in application.yml:

info:
    build:
        version: 0.0.1-SNAPSHOT  

Is this possible?

Upvotes: 4

Views: 8527

Answers (2)

Kevvvvyp
Kevvvvyp

Reputation: 1774

Add the following to the build.gradle file. It is not necessary to create an InfoContributor class.

plugins {
  id 'org.springframework.boot' version '<version>'
}

springBoot {
  buildInfo()
}

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

Query http://host:port/actuator/info and you will see something like...

{
  "build": {
    "artifact": "spring-example",
    "name": "spring-example",
    "time": "2020-01-30T09:47:33.551Z",
    "version": "0.0.1-SNAPSHOT",
    "group": "com.example"
  }
}

N.b. Tested with 2.2.4.RELEASE

Upvotes: 8

jst
jst

Reputation: 1757

This exact use case is spelled out in the Boot docs:

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info-automatic-expansion-gradle

So in your build.gradle do this

version = '0.0.1-SNAPSHOT'
processResources {
    expand(project.properties)
}

Your application.yml

info:
  build:
    version: ${version}
  1. Make sure to escape any spring placeholders so it doesn't conflict with Gradle. Both use ${} as the replacement token. ${key} should become \${key} . This will affect anything in src/main/resources.

  2. Clean/build and deploy your war/jar. Don't run it from your IDE or the placeholder won't be replaced.

Upvotes: 4

Related Questions