要替换Spring Boot application.yml
中的属性,我已添加:
processResources {
filesMatching("**/application.yml") {
expand(project.properties)
}
}
替换失败但提供MissingPropertyException
:
Caused by: groovy.lang.MissingPropertyException: No such property: OPENSHIFT_MYSQL_DB_HOST for class: SimpleTemplateScript1
at SimpleTemplateScript1.run(SimpleTemplateScript1.groovy:49)
at org.gradle.api.internal.file.copy.FilterChain$3.transform(FilterChain.java:95)
at org.gradle.api.internal.file.copy.FilterChain$3.transform(FilterChain.java:84)
at org.gradle.api.internal.ChainingTransformer.transform(ChainingTransformer.java:37)
at org.gradle.api.internal.file.copy.FilterChain.transform(FilterChain.java:39)
at org.gradle.api.internal.file.copy.FilterChain.transform(FilterChain.java:46)
at org.gradle.api.internal.file.copy.DefaultFileCopyDetails.open(DefaultFileCopyDetails.java:86)
at org.gradle.api.internal.file.AbstractFileTreeElement.copyTo(AbstractFileTreeElement.java:56)
at org.gradle.api.internal.file.copy.DefaultFileCopyDetails.copyTo(DefaultFileCopyDetails.java:94)
at org.gradle.api.internal.file.AbstractFileTreeElement.copyFile(AbstractFileTreeElement.java:93)
at org.gradle.api.internal.file.AbstractFileTreeElement.copyTo(AbstractFileTreeElement.java:74)
... 81 more
最初我的application.yml
包含:
url: jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/${OPENSHIFT_APP_NAME}
请注意,这些Openshift变量仅在Openshift生产环境中知道,但在开发模式下本地运行时不知道。
正如http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html所述:You can also include arbitrary Groovy code in the file, such as ${version ?: 'unknown'}
所以我将application.yml
更改为:
url: jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST ?: ''}:${OPENSHIFT_MYSQL_DB_PORT ?: ''}/${OPENSHIFT_APP_NAME ?: ''}
但这会给出相同的MissingPropertyException
。
我在这里错过了什么吗?
答案 0 :(得分:7)
Gradle expand
${..}
样式与相同的Spring属性占位符样式冲突,因此需要像\${..}
一样进行转义。
现在,它已添加到Spring Boot文档中:https://github.com/spring-projects/spring-boot/commit/c0c67f2593dbfd17aa304b43f4da3a3678fa58eb