Getting the Gradle.build version into Spring Boot

前端 未结 4 1707
广开言路
广开言路 2021-01-03 23:35

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 tr

相关标签:
4条回答
  • 2021-01-03 23:45

    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()

    0 讨论(0)
  • 2021-01-03 23:51

    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 ;)

    0 讨论(0)
  • 2021-01-04 00:04

    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.

    0 讨论(0)
  • 2021-01-04 00:09

    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}")
    
    0 讨论(0)
提交回复
热议问题