spring-bootgradle

Gradle processResources for Spring Boot application.yml and select property to expand


I have a gradle.properties which specifies the version

version=0.1.0

I want to expand the version in my Spring Boot application.yml below, however, I don't want to expand all properties, e.g. such as the SPRING_PROFILES_ACTIVE variable and various other ones. Some of those values are injected from a Kubernetes build tool.

spring:  
  profiles:
    active: ${SPRING_PROFILES_ACTIVE}
  application:
    version: ${version}

I know I can use the gradle task to do the entire file.

processResources {
    filesMatching("application.yml") {
      expand(project.properties)
    }
}

But is there a way to filter and only expand the version?


Solution

  • This code works for me:

    processResources {
      filesMatching("application.yml") {
        expand(["version": version])
      }
    }
    

    Here is an alternative method that avoids conflicts with Spring's ${} placeholders:

    application.yml:

        version: @project.version@
    

    build.gradle:

        import org.apache.tools.ant.filters.ReplaceTokens
        processResources {
            filesMatching(["application*.yml", "application*.yaml", "application*.properties"]){
                filter(ReplaceTokens, tokens: 
                [ "project.version": version],
                beginToken: '@', 
                endToken: '@'
                )
            }
        }