Git Commit Information in Spring MVC

Author: Shazin Sadakath


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

        
            org.springframework.boot
            spring-boot-actuator
            Version
        

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;

    
        
            
                
                    pl.project13.maven
                    git-commit-id-plugin
                    2.2.4
                    
                        
                            get-the-git-infos
                            
                                revision
                            
                            package
                        
                        
                            validate-the-git-infos
                            
                                validateRevision
                            
                        
                    

                    
                        ${project.basedir}/../.git
                        yyyy-MM-dd'T'HH:mm:ssZ
                        ${user.timezone}
                        true
                        ${project.build.outputDirectory}/git.properties
                        properties
                        true
                        false
                        true
                        true
                        flat
                        
                            false
                            true
                            -dirty
                            *
                            false
                            false
                        

                        
                            
                                validating project version
                                ${project.version}
                                
                            
                        
                    

                 
             
         
    

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.

 



Tags: Git SpringMVC SpringBoot SpringBootActuator
Views: 241

Comments

Please login or register to post a comment.


There are currently no comments.