Advertisement

Sri Lanka's First and Only Platform for Luxury Houses and Apartment for Sale, Rent

Tuesday, January 30, 2018

Git Commit Information in Spring MVC

At time there is a need for developers the need to know which Git Commit from which Git Branch was used to build a release so that they can keep track of the versions.
Spring Boot Actuator is tailor made for this if you are working on Spring Boot but if you are like me and not using Spring Boot for a specific project but only using Spring MVC then this guide will be helpful for you.
First of all you need to add the spring-boot-actuator
 <dependency>  
     <groupid>org.springframework.boot</groupid>  
     <artifactid>spring-boot-actuator</artifactid>  
     <version>Version</version>  
 </dependency>  
And then instead of relying on Spring Boot Autoconfiguration, in your Java Annotation configuration you can do the following;
@Configuration
@Import({
        EndpointWebMvcAutoConfiguration.class,
        ManagementServerPropertiesAutoConfiguration.class,
        EndpointAutoConfiguration.class
})
And in order to generate the git.properties file you can use the Maven git-commit-id plugin and use it as below;
 <build>  
     <pluginmanagement>  
       <plugins>  
         <plugin>  
           <groupid>pl.project13.maven</groupid>  
           <artifactid>git-commit-id-plugin</artifactid>  
           <version>2.2.4</version>  
           <executions>  
             <execution>  
               <id>get-the-git-infos</id>  
               <goals>  
                 <goal>revision</goal>  
               </goals>  
               <phase>package</phase>  
             </execution>  
             <execution>  
               <id>validate-the-git-infos</id>  
               <goals>  
                 <goal>validateRevision</goal>  
               </goals>  
             </execution>  
           </executions>  
           <configuration>  
             <dotgitdirectory>${project.basedir}/../.git</dotgitdirectory>  
             <dateformat>yyyy-MM-dd'T'HH:mm:ssZ</dateformat>  
             <dateformattimezone>${user.timezone}</dateformattimezone>  
             <generategitpropertiesfile>true</generategitpropertiesfile>  
             <generategitpropertiesfilename>${project.build.outputDirectory}/git.properties</generategitpropertiesfilename>  
             <format>properties</format>  
             <skippoms>true</skippoms>  
             <injectallreactorprojects>false</injectallreactorprojects>  
             <failonnogitdirectory>true</failonnogitdirectory>  
             <failonunabletoextractrepoinfo>true</failonunabletoextractrepoinfo>  
             <commitidgenerationmode>flat</commitidgenerationmode>  
             <gitdescribe>  
               <skip>false</skip>  
               <always>true</always>  
               <dirty>-dirty</dirty>  
               <match>*</match>  
               <tags>false</tags>  
               <forcelongformat>false</forcelongformat>  
             </gitdescribe>  
             <validationproperties>  
               <validationproperty>  
                 <name>validating project version</name>  
                 <value>${project.version}</value>  
                 <shouldmatchto>&lt;![CDATA[^.*(?&lt;!-SNAPSHOT)$]]&gt;</shouldmatchto>  
               </validationproperty>  
             </validationproperties>  
           </configuration>  
          </plugin>  
        </plugins>  
      </pluginmanagement>  
   </build>  
And when packaging you can use the following command
$ mvn clean package git-commit-id:revision 
Now when you access http://://info endpoint you will see the git commit id, branch name, commit time etc. and you can customize the pom.xml git-commit-id plugin configuration to have more information.