2017-09-15 80 views
0

我目前正在構建一個使用gradle作爲構建工具的Kotlin,Spring Boot服務。我試圖自動展開在我application.properties文件中找到的屬性,使用這裏找到步驟:在kotlin/spring引導中使用Gradle屬性擴展

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[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階段失敗。

回答

0

所以,有我的build.gradle的一小部分,我並沒有包括以上,因爲我不知道這件事情:

bootRun { 
    addResources = true 
} 

按照Spring Boot Docs具有addResources標誌設置爲true,將忽略在processResources步驟中創建的application.properties文件,而是使用項目源中的文件。這樣可以讓您動態更改文件而無需重新啓動應用程序。

這樣做的正確的修復(如果你需要與​​任務運行時,變量擴展)是設置:

bootRun { 
    addResources = false 
} 

否則,如果你建立你的罐子時,只需要擴張,離開這個標誌作爲true應該沒事。