23

The recommended way to do resource filtering in Gradle is by having tokens in the properties file and then replacing them when processing.

Example

# config.properties
hostname = @myhost@

and in build.gradle do something like below

processResources {
   filter ReplaceTokens, tokens: [
      "myhost": project.property('myhost')
   ]
}

The problem with this approach is that it won't work when running from IDEs like eclipse. I would like the property files to be free of Gradle specific tokens i.e just have

hostname = localhost

but have option to replace it when building from Gradle.

Krishnaraj
  • 2,360
  • 1
  • 32
  • 55

4 Answers4

30

You could use the following (not tested):

processResources {
    filesMatching('**/config.properties') {
        filter {
            it.replace('localhost', project.property('myhost'))
        }
    }
}

Or you could have a default file, used during development in your IDE, and have another file containing tokens and replacing the development one when building using gradle. Something like this (not tested)

processResources {
    exclude '**/config.properties'
    filesMatching('**/config-prod.properties') {
        setName 'config.properties'
        filter ReplaceTokens, tokens: [
            "myhost": project.property('myhost')
        ]
    }
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • In Gradle 6.2.2 I kept getting `org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String` when using `project.property('version')`. I had to change your example to `filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: ['version': project.property('version').toString()])`. – Elliot Vargas Mar 24 '20 at 15:10
6

Can use thing like placeholder if you want.

In config.properties file

var1=${var1}
var2=${var2}

In gradle.properties file

processResources {
    filesMatching('**config.properties') {
        expand(
            'var1': project.property('var1'),
            'var2': project.property('var2'),
        )
    }
}
Quy Tang
  • 3,929
  • 1
  • 31
  • 45
  • 1
    expand uses Groovy's SimpleTemplateEngine, and if there are unwanted $s in the files it will fail. – xing Aug 10 '16 at 07:13
  • @xing yup, but that's not too big problem. Because in that case, there are probably some errors right? – Quy Tang Aug 10 '16 at 09:21
1

The spring-boot approach

project.version=X.X.X.X
info.build.version=@project.version@

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info-automatic-expansion

Chomeh
  • 1,046
  • 13
  • 15
0
# File: application.yml
# forward gradle properties to spring boot properties
version: @version@

Setup gradle task (tested with Gradle 7.4):

import org.apache.tools.ant.filters.ReplaceTokens
processResources {
    with copySpec {
        from 'src/main/resources'
        include 'application*.yml'
        duplicatesStrategy 'include'
        project.properties.findAll {it.value != null}.each {
            filter(ReplaceTokens, tokens: [(it.key): it.value.toString()])
        }
    }
}

Resulting file:

# File: application.yml
# forward gradle properties to spring boot properties
version: 0.0.1-SNAPSHOT
IPP Nerd
  • 992
  • 9
  • 25