【问题标题】:Using Gradle property expansion in kotlin/spring boot在 kotlin/spring boot 中使用 Gradle 属性扩展
【发布时间】:2017-09-15 01:20:27
【问题描述】:

我目前正在使用 gradle 作为构建工具构建KotlinSpring 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 阶段会失败。

【问题讨论】:

    标签: spring-boot gradle kotlin


    【解决方案1】:

    所以,我的 build.gradle 有一小部分没有包含在上面,因为我不知道:

    bootRun {
        addResources = true
    }
    

    根据Spring Boot Docs,将 addResources 标志设置为 true 将忽略在 processResources 步骤中创建的 application.properties 文件,而是在项目源中使用该文件。这是为了让您无需重新启动应用程序即可动态更改文件。

    正确的解决方法是设置:

    bootRun {
        addResources = false
    }
    

    否则,如果您在构建 jar 时只需要扩展,则将此标志保留为 true 应该没问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 2018-10-19
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多