javaspringgradlespring-boot

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?


Solution

  • 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}")