Zarick Lau
Zarick Lau

Reputation: 1615

Gradle copy task expand with properties derived at execution time

In gradle, I am currently doing something like this:

task generateConfiguration(type: Copy) {
   into "$buildDir/generatedConfiguration" 
   from "src/main/config-templates" 
   expand(loadConfig())
}

loadConfig() is using Groovy ConfigSlurper to load Groovy based config and it return a Map for the "expand" method to consume.

This approach works find with one caveat.

the loadConfig() is invoked during configuration phase.

If I would like to defer the loadConfig() process until execution phase, I cannot do it with this approach. expand() method doesn't accept closure.

Any good sugguestion so that I can defer the loadConfig into execution phase?

I have been looking for a simple solution for awhile already with no luck. Right now, I have only two possible routes: 1) implement a map object which will defer the loadConfig() operation until the map is accessed by the template engine 2) reimplement the copy task so that it can take a closure.

I'm trying to look for a simpler options though..

Upvotes: 1

Views: 1148

Answers (1)

Opal
Opal

Reputation: 84786

Maybe:

task generateConfiguration {
   doLast {
      copy {
         into "$buildDir/generatedConfiguration" 
         from "src/main/config-templates" 
         expand(loadConfig())
      }
   }    
}

This way the parameters should be expanded at execution phase.

Upvotes: 2

Related Questions