Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将Gradle.build版本导入Spring Boot_Java_Spring_Gradle_Spring Boot - Fatal编程技术网

Java 将Gradle.build版本导入Spring Boot

Java 将Gradle.build版本导入Spring Boot,java,spring,gradle,spring-boot,Java,Spring,Gradle,Spring Boot,我试图在视图中显示Spring Boot应用程序的应用程序版本。我确信我可以访问此版本信息,我只是不知道如何访问 我尝试了以下信息:,并将其放入我的应用程序中。属性: info.build.version=${version} info.build.version=whatever 然后在我的控制器中加载它@Value(${version.test}”),但这不起作用,我只会得到如下错误: Caused by: java.lang.IllegalArgumentException: Coul

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

我尝试了以下信息:,并将其放入我的
应用程序中。属性

info.build.version=${version}
info.build.version=whatever
然后在我的控制器中加载它
@Value(${version.test}”)
,但这不起作用,我只会得到如下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in string value "${version}"
关于如何正确地将我的应用程序版本、spring boot版本等信息获取到我的控制器中,您有什么建议吗?

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

processResources {
    expand(project.properties)
}
为了安全起见,您可能希望缩小范围,以便只处理
application.properties

processResources {
    filesMatching('application.properties') {
        expand(project.properties)
    }
}
现在,假设您的属性名为
info.build.version
,则可以通过
@Value

@Value("${info.build.version}")
我是这样解决的: 在
application.properties
中定义您的
info.build.version

info.build.version=${version}
info.build.version=whatever
在组件中使用它

@Value("${info.build.version}")
private String version;
现在将您的版本信息添加到
build.gradle
文件中,如下所示:

version = '0.0.2-SNAPSHOT'
然后添加一个方法,用正则表达式替换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')
}
最后,确保在触发
build
bootRun
时调用该方法:

allprojects {
    updateVersion()
}
就这样。如果您让Gradle编译您的应用程序,以及从IDE运行Spring Boot应用程序,则此解决方案可以工作。该值不会得到更新,但不会引发异常,一旦运行Gradle,它将再次更新


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

我通过在application.yml中添加以下内容来解决此问题:

${version?:unknown}
它还可以从cli:gradle bootRun和IntelliJ运行,在IntelliJ中启动或使用spring配置文件之前,您不必调用gradle任务processResources

这适用于Gradle版本:4.6和Spring Boot版本:2.0.1。发行版
希望有帮助;)

您也可以在
build.gradle
中添加此项:

springBoot {    
    buildInfo() 
}
然后,您可以使用
BuildProperties
bean:

@Autowired
private BuildProperties buildProperties;

并使用
buildProperties.getVersion()

获取版本。您想从spring boot应用程序以编程方式访问gradle构建脚本变量(如版本)?@RaGe是的,这就是我想要的。@Eric您最终做到了吗?嗨,Andy,谢谢您的回复。我已经试过了,但是每当我从IDE运行Spring启动应用程序时,我都会遇到错误:无法解析字符串值“${version}”中的占位符“version”。我将其添加到我的application.properties:
info.build.version=${version}
,并将其添加到我的控制器:
@Value(${info.build.version}”)私有字符串applicationVersion
@ErikPragt根据您使用的IDE以及您导入Gradle项目的方式,Gradle可能没有运行,因此它没有机会处理您的application.properties文件。它在命令行上工作吗(例如,
/gradlew bootRun
)?是的,我当然有点傻,我是从IDE而不是从Gradle运行它的。有没有一种方法可以让它一直有效?例如,Spring Boot的ResourceBanner.java做了类似的事情……@AndyWilkinson:你能帮我写这篇文章吗。这打破了集成测试。对我来说效果很好,谢谢。我更喜欢这个,而不是ProcessResources我尝试了这里发布的所有解决方案,这是目前为止最好的解决方案。因此,请毫不犹豫地升级这个解决方案,使它对其他人更可见:)如果您使用的是Eclipse IDE,那么该插件可能会有一些困难。我已经为这个问题写了一篇文章,这是迄今为止最好的解决方案