简体   繁体   中英

Getting the Gradle.build version into Spring Boot

I'm 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.

I tried following this 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}") in my controller, but that doesn't work, I only get errors like:

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

Any suggestions on what the proper way to get information like my app version, the spring boot version, etc into my controller?

You can also add this in build.gradle :

springBoot {    
    buildInfo() 
}

Then, you can use BuildProperties bean:

@Autowired
private BuildProperties buildProperties;

And get the version with buildProperties.getVersion()

As described in the reference documentation , you need to instruct Gradle to process you application's resources so that it will replace the ${version} placeholder with the project's version:

processResources {
    expand(project.properties)
}

To be safe, you may want to narrow things down so that only application.properties is processed:

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

Now, assuming that your property is named info.build.version , it'll be available via @Value :

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

I've resolved this by adding into application.yml the following:

${version?:unknown}

It also work from cli: gradle bootRun and also from IntelliJ and you don't have to call the Gradle task processResources before launching in IntelliJ or use spring profiles.

This work with Gradle ver: 4.6 and also Spring Boot ver: 2.0.1.RELEASE . Hope it helps;)

I have solved it this way: 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 info to your build.gradle file like this:

version = '0.0.2-SNAPSHOT'

then add a method to replace your application.properties with a regex to update your 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, ensure the method is called when you trigger build or bootRun :

allprojects {
    updateVersion()
}

that's it. This solution works if you let Gradle compile your app as well as if you run your Spring Boot app from the IDE. The value will not get updated but won't throw an exception and as soon as you run Gradle it will be updated again.

I hope this helps others as well as it solved the problem for me. I couldn't find a more proper solution so I scripted it by myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM