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.

Top articles
Servlet interview questions and answers for experienced Published at:- Spring Interview Questions and Answer for Experienced Published at:- Spring Boot Features for Java Developers (2022) Published at:- Detecting build version and time at runtime in Spring Boot Published at:- AWS Interview Questions Answers for Freshers and Experienced Published at:- Laravel Interview Question Answer for Freshers and Experienced Published at:- Splunk Interview Questions and Answers for freshers and Experienced Published at:- IOS Interview Questions for freshers and experienced Published at:- Red Hat Interview Question Answers for Freshers and experienced Published at:- Python Interview Question and Answer Published at:- Basic Indian History General Knowledge Question and Answer (Quiz) Published at:- AWS Interview Question and Answer for freshers and Experienced Published at:- Angular 13 interview Question and Answer Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination (Set 2) Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination (Set 3) Published at:- Limited Departmental Competitive examination (LDC) examination (Water supply) Published at:- Interview Question and answer for Limited Departmental Competitive Examination Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination (Soil mechanics) Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination Sanitary Engineering Published at:- Interview Question for Limited Departmental Competitive Examination (LDC) Principles of Surveying Published at:- Interview MCQ question for railways New Lines, Doublings & Gauge Conversion Projects Published at:- MCQ Interview Question and answer for Railway Station and Passenger Amenities Published at:- Interview Question and Answer for Railway preparation Published at:- PHP MCQ Quiz Question with Answer Published at:- PHP MCQ Quiz Question with Answer (set 2) Published at:- Angular MCQ Quiz Question and Answer Published at:- Angular MCQ Quiz Question and Answer Set 2 Published at:- Java General Interview Questions and Answer Published at:- Java Threads Interview Question and Answer for experienced Published at:- Java Collections Interview Question and Answer for freshers and experienced Published at:- Garbage Collector in Java Frequently Asked Questions and Answers Published at:- Difference between Exception and Error in java Exception Handling Published at:- Java Applets Interview Questions with Answer Published at:- Java Swing Interview Questions with Answer for freshers and experienced Published at:- Java Database Connectivity (JDBC) Interview Question and answer Published at:- Remote Method Invocation (RMI) Interview Question and Answer Published at:- Jakarta Server Page (JSP) programming language Interview Question and Answer Published at:- Write a program in Java to display the first 10 terms of the following series: 10, 20, 30, 40, …….. Published at:- Write the program in Java to display the first ten terms of the following series: 1, 4, 9, 16, Published at:-
R4Rin Team
The content on R4Rin.com website is created by expert teams.