How can I replace tokens in a file in a copy task using values that were computed by another task?

I have one task (copyFiles) that copies files and replaces tokens in them. I know how to use the ReplaceTokens filter in a copy task. However, in my case, I have another task (computeValues) that computes the values to use for replacing the tokens and stores them in local variables or project properties. This does not work since the expression in my filter definition is evaluated during the configuration phase and at that time the variables/project properties are not set yet as computeValues has not been executed yet.

Something like this:

def tokenValue
  task computeValues << {
   tokenValue = ... whatever computation
}
    task copyFiles(type: Copy, dependsOn: [computeValues]) {
  from("template"){
      include "*.sh"
      // does not work as tokenValue is still null when this is evaluated
      filter(ReplaceTokens, tokens: [myToken: tokenValue])
  }
}

Is there a way to achieve what I want?

Try placing the call to ‘filter’ inside a closure which is evaluated at execution time.

eachFile {

filter(ReplaceTokens, tokens: [myToken: tokenValue])

}

Thanks. I now ended up doing it this way but I guess the main thing is to have a closure so the variable is evaluated at execution time as you wrote.

from("appTemplate"){
      include "*.sh"
      filter{ it.replaceAll('@@libVersion@@', libVersion)}
  }