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.

 

Contents

 

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

<plugin>
 <groupId>pl.project13.maven</groupId>
 <artifactId>git-commit-id-plugin</artifactId>
 <version>2.2.4</version>
 <executions>
 <execution>
 <goals>
 <goal>revision</goal>
 </goals>
 </execution>
 </executions>
 <configuration>
 <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
       <includeOnlyProperties>
        <includeOnlyProperties>^git.branch$</includeOnlyProperties>
        <includeOnlyProperties>^git.commit.id$</includeOnlyProperties>
         <includeOnlyProperties>^git.commit.time$</includeOnlyProperties>
         <includeOnlyProperties>^git.build.time</includeOnlyProperties>
       </includeOnlyProperties>
 
 <generateGitPropertiesFile>true</generateGitPropertiesFile>
   <failOnNoGitDirectory>false</failOnNoGitDirectory>
 </configuration>
</plugin>

After we build the application a git.properties file will be generated.


Access the information in code

When the jar/war is created the git.properties file is generated and placed where the application.properties is placed under resources directory. I have written the AppInfo class that can access the properties from the git.properties 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;

@Configuration
@PropertySources({@PropertySource("classpath:git.properties")}) 
public class AppInfo {

  @Value("${git.commit.id}")
  private String commitId;

  @Value("${git.build.time}")
  private String buildTime;

  @PostConstruct
  public void init() { 
      print();
  }

  public void print() {
logger.info(“commitId {}”,commitId);
logger.info(“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,388 views0 comments

Recent Posts

See All
bottom of page