Gradle does not mark ant task as UP-TO-DATE


(Moritz) #1

Hey guys,

we’re generating some code with the help of an ant task as described in this post: http://joerglenhard.wordpress.com/2012/01/10/xjc-and-schemagen-with-gradle/

Everything works fine with this task and gradle marks the task as UP-TO-DATE when I run it a second time:

task osbConfigSources {
    inputs.file "${osbConfigSchema}"
    outputs.dir "${buildDir}/generated-sources/osb"
    doLast {
        file("${buildDir}/generated-sources/osb").mkdirs()
        ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.xjc.asPath)
        ant.xjc(destdir: "${buildDir}/generated-sources/osb", schema: "${osbConfigSchema}", package: "${osbConfigPackage}")
    }
}

What I don’t understand is why it doesn’t with this code:

task osbConfigSources {
    doFirst {
        inputs.file "${osbConfigSchema}"
        outputs.dir "${buildDir}/generated-sources/osb"
        file("${buildDir}/generated-sources/osb").mkdirs()
    }
    ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.xjc.asPath)
    ant.xjc(destdir: "${buildDir}/generated-sources/osb", schema: "${osbConfigSchema}", package: "${osbConfigPackage}")
}

I’d prefer the second solution as it’d be doing the prerequisites in the doFirst block but with this solution gradle does not mark the task as UP-TO-DATE but executes it everytime.

Why?

Cheers,

Moritz


(René Groeschke) #2

Hey Moritz,

the intputs.file and outputs.file properties should be set during configuration time. in your second example. these props are set during the task action. that’s simply to late as the task is already executing.

cheers, René


(Moritz) #3

Hey Rene,

you’re right. Totally mixed that one up - early Monday morning :slight_smile:

So the finally working solution now looks like this:

task osbConfigSources {
    inputs.file "${osbConfigSchema}"
    outputs.dir "${buildDir}/generated-sources/osb"
      doFirst{
        file("${buildDir}/generated-sources/osb").mkdirs()
    }
      ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.xjc.asPath)
    ant.xjc(destdir: "${buildDir}/generated-sources/osb", schema: "${osbConfigSchema}", package: "${osbConfigPackage}")
}

BUT there’s a new question now: Even though I deleted some of the generated classes, gradle marks the task as UP-TO-DATE. Nevertheless it generated the missing classes again.

Cheers,

Moritz


(Moritz) #4

Hey Rene,

you’re right, I totally mixed that one up - early Monday morning :wink:

So a working solution now looks like this:

task osbConfigSources {
    inputs.file "${osbConfigSchema}"
    outputs.dir "${buildDir}/generated-sources/osb"
    ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.xjc.asPath)
      doFirst {
        file("${buildDir}/generated-sources/osb").mkdirs()
    }
      doLast{
        ant.xjc(destdir: "${buildDir}/generated-sources/osb", schema: "${osbConfigSchema}", package: "${osbConfigPackage}")
    }
}

Cheers,

Moritz