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.
source share