java 将 Gradle.build 版本导入 Spring Boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34963056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:35:22  来源:igfitidea点击:

Getting the Gradle.build version into Spring Boot

javaspringgradlespring-boot

提问by Erik Pragt

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.

我正在尝试在视图中显示我的 Spring Boot 应用程序的应用程序版本。我确定我可以访问此版本信息,只是不知道如何访问。

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:

我尝试遵循以下信息:https: //docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html,并将其放入我的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:

然后将它加载@Value("${version.test}")到我的控制器中,但这不起作用,我只会收到如下错误:

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?

关于将我的应用程序版本、spring boot 版本等信息获取到我的控制器中的正确方法有什么建议吗?

采纳答案by Andy Wilkinson

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:

参考文档中所述,您需要指示 Gradle 处理您的应用程序资源,以便它将${version}占位符替换为项目的版本:

processResources {
    expand(project.properties)
}

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

为安全起见,您可能希望缩小范围,以便仅application.properties处理:

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

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

现在,假设您的财产名为info.build.version,它将通过@Value以下方式提供:

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

回答by brebDev

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

我通过在 application.yml 中添加以下内容解决了这个问题:

${version?:unknown}

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

它也适用于 cli: gradle bootRun和 IntelliJ,并且您不必在 IntelliJ 中启动或使用 spring 配置文件之前调用 Gradle 任务 processResources。

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

这适用于 Gradle ver: 4.6和 Spring Boot ver: 2.0.1.RELEASE。希望能帮助到你 ;)

回答by Clément Poissonnier

You can also add this in build.gradle:

您还可以将其添加到build.gradle

springBoot {    
    buildInfo() 
}

Then, you can use BuildPropertiesbean :

然后,您可以使用BuildPropertiesbean :

@Autowired
private BuildProperties buildProperties;

And get the version with buildProperties.getVersion()

并获得版本 buildProperties.getVersion()

回答by user3105453

I have solved it this way: Define your info.build.versionin application.properties:

我已经解决了这个问题:定义你info.build.versionapplication.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.gradlefile like this:

现在将您的版本信息添加到您的build.gradle文件中,如下所示:

version = '0.0.2-SNAPSHOT'

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

然后添加一个方法用正则表达式替换您的 application.properties 以更新您的版本信息:

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 buildor bootRun:

最后,确保在触发build或时调用该方法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.

而已。如果您让 Gradle 编译您的应用程序以及从 IDE 运行您的 Spring Boot 应用程序,则此解决方案有效。该值不会更新,但不会引发异常,一旦您运行 Gradle,它将再次更新。

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.

我希望这对其他人有所帮助,同时也为我解决了问题。我找不到更合适的解决方案,所以我自己编写了脚本。