Detecting build version and time at runtime in Spring Boot
Categories: Interview questions and answers Spring Boot
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
Itinstructs the plugin to execute also build-info goal, which is not run bydefault. This generates build meta-data about your application, which includesartifact version, build time and more.
springBoot{
buildInfo()
}
AccessingBuild Properties
Afterconfiguring your spring-boot-maven-plugin and building your application, youcan access information about your application's build through BuildPropertiesobject. Let the Spring inject it for you:
@Autowired
BuildPropertiesbuildProperties;
Nowyou can access various information from this object.
//Artifact's name from thepom.xml file
buildProperties.getName();
//Artifact version
buildProperties.getVersion();
//Date and Time of thebuild
buildProperties.getTime();
//Artifact ID from thepom file
buildProperties.getArtifact();
//Group ID from the pomfile
buildProperties.getGroup();
Youcan access custom properties defined this way by calling buildProperties.get("property.name").
ForGradle projects, custom properties can be defined this way:
springBoot{
buildInfo {
properties {
additional = [
'property.name': 'propertyvalue',
'other.property':'different.value'
]
}
}
}
Howit works under the hood
Whenbuild-info of spring-boot-maven-plugin isrun, it generates a property filecontaining all the build information. Bydefault, it is locatedat${project.build.outputDirectory}/META-INF/build-info.properties, but youcancustomize it by providing outputFile parameter. The file looks somethinglikethis:
build.version=0.0.1-SNAPSHOT
build.group=com.vojtechruzicka
build.name=spring-rest-docs-example
build.artifact=spring-rest-docs-example
build.time=2018-06-23T13\:58\:56.742472800Z
WhenSpring detects there is this file on theclasspath, it creates BuildPropertiesbean unless it is explicitly declared.This is configuredinorg.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration.Thisis a nice example of Spring Boot Auto-Configuration, where certain beanscan becreated just by having specific files on the classpath.
@ConditionalOnResource(resources=
"${spring.info.build.location:classpath:META-INF/build-info.properties}")
@ConditionalOnMissingBean
@Bean
public BuildPropertiesbuildProperties() throws Exception {
return new BuildProperties(
loadFrom(this.properties.getBuild().getLocation(),
"build"));
}
DetectingSpring profiles
Itis no doubt useful to know which version ofyour artifact is deployed and whenit was built. However, it is usually notenough. Often Spring applications usevarious profiles, which can significantlychange the behavior. Typical usage is,for example, having a separate profilefor each environment (DEV, UAT, PROD,...). Depending on the profile, thecorrect environmental configuration can beloaded such as DB connection andmore.
Itis useful to be able to determine currentprofiles as sometimes the app can berun with different profiles than expected.To detect the current profiles, youneed just to inject Environment object andthen you can simply obtain them bycalling getActiveProfiles().
@Autowired
private Environment environment;
environment.getActiveProfiles();
What'smore, since you already haveenvironment object, you can obtain anyenvironmental properties bycallingenvironment.getProperty("property.name").
SpringActuator & Admin
Whilethis approach gives you basic build andversion info, sometimes you may need amore powerful tool. Spring Boot Actuatoris a sub-project of Spring Boot, whichadds some production-grade monitoringand management tools exposed as REST andJMX endpoints. In fact, it can easilybe configured to use build informationprovided by BuildProperties and providethem through one of its endpoints.
SpringBoot Admin is a community project,which provides a nice user interface on topof Spring Actuator endpoints, sothe app is more comfortable to manage through anice admin GUI.
Conclusion
Havingaccess to version and build informationat runtime can be quite useful. InSpring boot application, you can easilyobtain the info by altering the SpringBoot Maven/Gradle plugin configurationto generate the build.properties file andthen accessing it throughBuildProperties object.