Spring Sweets: Add Git Info to Info Endpoint
A simple change to the Gradle configuration can enhance your Spring Boot information endpoint with extra Git information.
Join the DZone community and get the full member experience.
Join For FreeWith Spring Boot Actuator, we get some endpoints that display information about our application. One of the endpoints is the /info
endpoint. If our project uses Git, we can add information about Git to the /info
endpoint. By default, Spring Boot will look for a file git.properties
in the classpath of our application. The file is a Java properties file with keys that start with git.
and have values like the branch name, commit identifier, and commit message.
Spring Boot uses this information, and when we request the /info
endpoint, we get a response with the information. This can be very useful to check the Git information that was used to build the application. To create the git.properties
file, we can use a Gradle (or Maven) plugin that will do the work for us.
In the following example, we use the Gradle plugin to generate the git.properties
file for our project. The Gradle Git properties plugin is added in the plugins
configuration block. The plugin adds a Gradle extension gitProperties
that can be used to customize the output in git.properties
. We could even change the location (but we keep it to the default location build/resources/main/git.properties
).
// File: build.gradle
plugins {
id 'org.springframework.boot' version '1.4.2.RELEASE'
// Add Git properties plugin.
id 'com.gorylenko.gradle-git-properties' version '1.4.17'
}
// Customize Git properties plugin.
gitProperties {
// Change date format in git.properties file.
dateFormat = "yyyy-MM-dd HH:mm:ssZ"
dateFormatTimeZone = 'GMT'
}
That is all we need to do. The Git properties plugin generates the git.properties
file that is need by Spring Boot. Let's run our application and send a request for the /info
endpoint:
$ http -b localhost:8080/info
{
"git": {
"branch": "feature/dsl-hapi",
"commit": {
"id": "511d269",
"time": "2016-12-15 12:28:20+0000"
}
}
}
$
We can even show more information by changing the Spring Boot configuration property management.git.info.mode
. The default value is SIMPLE
, but we can also use the value FULL
. Then, we get the Git commit message and user details. We can set the configuration property in different ways for our Spring Boot application (for example, in application.yml
, Java system property or environment variable). In our example, we add a Java system property to the Gradle bootRun
task:
// File: build.gradle
...
bootRun {
systemProperty 'management.info.git.mode', 'FULL'
}
...
We execute the bootRun
task and send a new request for the /info
endpoint:
$ http -b localhost:8080/info
{
"git": {
"branch": "feature/dsl-hapi",
"commit": {
"id": "511d2693cbabc34449f838bf856fa2b9989cbca6",
"id.abbrev": "511d269",
"message": {
"full": "Enable Codenarc checks for integration test code",
"short": "Enable Codenarc checks for integration test code"
},
"time": "2016-12-15 12:28:20+0000",
"user": {
"email": "mrhaki@server",
"name": "Hubert Klein Ikkink"
}
}
}
}
$
This time, we see more information about Git.
Written with Spring Boot 1.4.2.RELEASE.
Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments