"Recursive" ReplaceTokens?


I’m trying to mimic a very specific behavior or the old ant copy task, more specifically the “recurse” attribute of the filterset (true by default) which allows tokens to be analyzed recursively to see if their value contained a token itself and re-evaluate it. For example, if I have two values defined at [token1=’/var’ , token2=’@token1@/tmp’] using @token2@ in a file would result in ‘/var/tmp’, not ‘@token1@/tmp’.

I couldn’t find anything close to that in the Gradle documentation, the catch might be that “recuse” was neither an attribute of Copy nor the ReplaceTokens classes even in the old ant, but a property of the filterset itself which I cannot find an equivalent in Gradle.

Thank you for any help/guidance you could provide.

If you’re doing something like this:

processResources {
    filter(ReplaceTokens, tokens: [token1: "/var", token2: "@token1@/tmp"])

You can instead do it like this:

processResources {
    def token = '/var'
    filter(ReplaceTokens, tokens: [token1: token, token2: "${token}/tmp"])

The ${token} gets replaced with the value of token via Groovy string interpolation.

We are actually loading the token values from a .properties file (which is using the same @token@ syntax internally), however I haven’t tried to use the Groovy string interpolation within the .properties file itself; would be worth it to give it a try :). The only downside to this is that it would require us to revisit all our existing .properties file as we migrate from Ant to Gradle.