在kotlin/spring引导中使用Gradle属性扩展
问题描述:
我目前正在构建一个使用gradle作为构建工具的Kotlin
,Spring Boot
服务。我试图自动展开在我application.properties
文件中找到的属性,使用这里找到步骤:在kotlin/spring引导中使用Gradle属性扩展
我的版本如下:
- kotlin: 1.1.4-3
- spring boot: 1.5.6.RELEASE
- gradle: 3.5.1
当我运行一个./gradlew bootRun
,我得到的以下错误:
java.lang.IllegalArgumentException: Could not resolve placeholder 'myServiceName' in value "${myServiceName}"
其次:
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: org.springframework.boot[email protected]510aeb93: startup date [Fri Sep 15 10:59:51 AEST 2017]; parent: org.spring[email protected]68edf5bb
的build.gradle:
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'org.springframework.boot'
processResources {
filesMatching('application.properties') {
expand(project.properties)
}
}
gradle.properties:
myServiceName=potato-service
application.properties:
info.app.name=${myServiceName}
这些错误出现在应用程序启动后,并且spring正在尝试加载属性文件。
有趣的是,如果我在application.properties中更改了要替换的变量,例如myServiceName123
,gradle在processResources阶段失败。
答
所以,有我的build.gradle的一小部分,我并没有包括以上,因为我不知道这件事情:
bootRun {
addResources = true
}
按照Spring Boot Docs具有addResources标志设置为true,将忽略在processResources步骤中创建的application.properties文件,而是使用项目源中的文件。这样可以让您动态更改文件而无需重新启动应用程序。
这样做的正确的修复(如果你需要与任务运行时,变量扩展)是设置:
bootRun {
addResources = false
}
否则,如果你建立你的罐子时,只需要扩张,离开这个标志作为true
应该没事。