在kotlin / spring引导中使用Gradle属性扩展
我目前正在使用gradle作为构建工具来构建Kotlin
, Spring Boot
服务。 我试图自动扩展在我的application.properties
文件中找到的属性,使用在这里找到的步骤:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-automatic-expansion-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.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@510aeb93: startup date [Fri Sep 15 10:59:51 AEST 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@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文件,而是使用项目源中的application.properties文件。 这是为了让您动态更改文件,而无需重新启动应用程序。
对此的正确解决方法(如果在运行bootRun
任务时需要可变扩展)将设置:
bootRun { addResources = false }
否则,如果你在构建你的jar时只是需要扩展,把这个标记为true
应该没问题。