top of page

Generate git commit and build information in Spring

Updated: Aug 1, 2021

Troubleshooting when applications do not behave as expected is part of a developer's life. And sometimes it's critical to know what is the version of the application, what is deployed, what was the last build time and last commit id. This information helps zeroing out the potential code that introduced a bug. Today I will show how we can extract those information and print them as part of the application log.




Using git commit id plugin

We are going to use the maven plugin git-commit-id-plugin for this purpose. You need to update your pom file with the below entry. In the configuration we instructed the plugin where to find the .git directory in the dotGitDirectory directive and then we instructed the plugin what information we would like to capture under the includeOnlyProperties directive. We have also instructed that the captured information will be saved in a git properties file. For that reason we have used the generateGitPropertiesFile directive. In case the .git directory is not found we don't want the build to fail in that case the information is simply not generated. The failOnNoGitDirectory directive with value false instructs that to the plugin. Below an example pom configuration is shown. For details refer to the git commit id plugin documentation.

An example pom


After we build the application a file will be generated.

Access the information in code

When the jar/war is created the file is generated and placed where the is placed under resources directory. I have written the AppInfo class that can access the properties from the file and put them into an object variable. This is basically a spring Configuration class that accesses the properties file and populates its object variables. It prints the information in the application log after the values are being populated from the properties file to the variables.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import javax.annotation.PostConstruct;

public class AppInfo {

  private String commitId;

  private String buildTime;

  public void init() { 

  public void print() {“commitId {}”,commitId);“buildTime {}”,buildTime);  

We have learned in a very simple way how we can print the git commit and build information in a spring boot application. You can refer to this blog that shows how to expose git commit information through a REST endpoint.

1,522 views0 comments

Recent Posts

See All


bottom of page