How to show git tag on application startup in Spring Boot?

My Spring Boot application uses Gradle. And I want to display latest git tag (which represents my application version) during application startup.

What I came up with, was quite an easy solution. Version information can be picked up from MANIFEST.MF file, that’s inside JAR. There is a property called Implementation-Version, which could be injected into custom generated banner.txt file, that’s picked up on application startup.

You need to modify your build.gradle file like so:

plugins {
  // Automatically generate git.properties file when building application
  // Please see: https://github.com/n0mer/gradle-git-properties
  id 'com.gorylenko.gradle-git-properties' version '2.4.1'
}

// ...

// Expose custom Git properties
gitProperties {
	extProperty = 'gitProps'
}

bootJar {
        // Add latest git tag to MANIFEST.MF FILE
	manifest {
		attributes 'Implementation-Version': "${-> project.ext.gitProps['git.closest.tag.name']}"
	}
}

And then simply create a cool banner.txt, place it under src/main/resources with added tags:



┓┏  ┓┓    ┓ ┏    ┓ ┓
┣┫┏┓┃┃┏┓  ┃┃┃┏┓┏┓┃┏┫
┛┗┗ ┗┗┗┛  ┗┻┛┗┛┛ ┗┗┻
                    
Application version: ${application.formatted-version}
Spring Boot version: ${spring-boot.formatted-version}

The result will be cool – application version as git tag + Spring Boot version.

If you need more information, please take a look:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *