Getting Gradle.build Version in Spring Download

I am trying to display the application version of my Spring Boot application in a view. I'm sure I can access this version information, I just don't know how to do this.

I tried the following information: https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html and put this in my application.properties :

 info.build.version=${version} 

And then loading it @Value("${version.test}") into my controller, but this will not work, I only get errors such as:

 Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in string value "${version}" 

Any suggestions on how to get the information correctly, such as my application version, Spring boot version, etc. in my controller?

+6
source share
2 answers

As described in the reference documentation , you need to instruct Gradle to process application resources so that it replaces the ${version} Placeholder with the project version:

 processResources { expand(project.properties) } 

To be safe, you can restrict everything so that only application.properties handled:

 processResources { filesMatching('application.properties') { expand(project.properties) } } 

Now, assuming your property has the name info.build.version , it will be available through @Value :

 @Value("${info.build.version}") 
+5
source

I solved it like this: Define your info.build.version in application.properties :

 info.build.version=whatever 

use it in your component with

 @Value("${info.build.version}") private String version; 

now add your version information to your build.gradle file as follows:

 version = '0.0.2-SNAPSHOT' 

then add a method to replace your application.properties with a regular expression to update the version information there:

 def updateApplicationProperties() { def configFile = new File('src/main/resources/application.properties') println "updating version to '${version}' in ${configFile}" String configContent = configFile.getText('UTF-8') configContent = configContent.replaceAll(/info\.build\.version=.*/, "info.build.version=${version}") configFile.write(configContent, 'UTF-8') } 

finally, make sure that the method is called when you run build or bootRun :

 allprojects { updateVersion() } 

what he. This solution works if you enable Gradle to compile your application, and also if you run the Spring boot application from the IDE. The value will not be updated, but will not throw an exception, and as soon as you run Gradle, it will be updated again.

I hope this helps others and also solves the problem for me. I could not find a more correct solution, so I wrote it myself.

0
source

All Articles