Keyword expansion doesn't play well with dynamic tasks

Hello crews,

I’d like to include some Mercurial information in the compiled software so it can be used later for improving customer’s support.

My build script should pick up a java template, replace some properties inside of it with actual values from Mercurial VCS and copy the transformed file into the build directory where it can be compiled later. This task depends on two dynamically created tasks - hg_build and hg_revision - querying Mercurial for build and revision information respectively. Two dedicated ext properties - programBuild and programRevision - should receive the actual values.

Here is the dedicated code:

['revision' : 'rev', 'build' : 'node|short'].each { dict ->
    task "hg_$dict.key"(type:Exec) {
        commandLine "hg log -r tip --template '{$dict.value}'".split(' ')
        standardOutput = new ByteArrayOutputStream()
        doLast {
            project.ext."program${dict.key.capitalize()}" = standardOutput.toString().replaceAll("'", '')

And, finally, the code that hardcodes Mercurial information into the template using keyword expansion:

task generateProgramId(type: Copy, dependsOn: hg_build) {
    from 'config'
    include '*.java'
    into file("${buildDir}/generated-src")
        companyName     : "${project.ext.companyName}",
        companyId       : "${project.ext.companyId}",
        programName     : "${project.ext.programName}",
        programVersion  : "${project.ext.programVersion}",
        programBuild    : "${project.ext.programBuild}"

The first three properties are defined at the head of the build file. The last two - programVersion and programBuild - should be set during execution of hg_build and hg_revision tasks.

Unfortunately, the code shown above doesn’t work as expected. It fails with the following error:

Cannot get property ‘programBuild’ on extra properties extension as it does not exist

It looks like the properties for the keyword expansion will be evaluated BEFORE the task’s dependency - hg_build! Debug options don’t give me any useful information on this failure.

Do you have any idea what’s going wrong in my code?

Thank you in advance!
Best regards