how to skip placeholder value if not supplied in properties file in spring boot?

前端 未结 3 1944
终归单人心
终归单人心 2020-12-22 03:52

I have a spring boot application that uses gradle. I have configured gradle to inject build parameters into application.properties.

Here is my gradle snippet.

<
相关标签:
3条回答
  • 2020-12-22 04:13

    I have the same problem - really annoying. Best solution I have found so far is to save a run/debug configuration in IntelliJ and then go in an edit the configuration, specifying that the gradle task processResources should be executed before launch.

    0 讨论(0)
  • 2020-12-22 04:14

    I've resolved this by adding into application.yml the following:

    ${version?:unknown}
    

    It also work from cli:gradle bootRun and also from IntelliJ and you don't have to call the Gradle task processResources before launching in IntelliJ or use spring profiles.

    This work with Gradle ver:4.6 and also Spring Boot ver: 2.0.1.RELEASE.
    Hope it helps ;)

    0 讨论(0)
  • 2020-12-22 04:24

    I solve this with Spring profiles. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

    In application.properties I have

    service.version=dev
    

    In application-production.properties I have

    service.version=${version}
    

    And finally this in the build.gradle file

    processResources {
        filesMatching('application-production.properties') {
            expand(project.properties)
        }
    }
    

    In dev, test and integration tests the version will be "dev", when running with the production profile it will use the build version.

    This also works when running the application directly from IntelliJ.

    0 讨论(0)
提交回复
热议问题