Exec task fails if executed code is in a do last segment

I’m trying to specify some input/output locations in order to only run a script when a file has been modified since the last build. Following the docs I’ve come up with.

task generateLocalizedStrings(type:Exec) {

ext.srcFile = file(’…/…/…/localization/language-files/en_US/wdmobilestringres.properties’)

ext.destDir = new File(“src/main/res”)

inputs.file srcFile

outputs.dir destDir

doLast {

workingDir ‘…/…/…/localization/’

commandLine ‘python’, ‘localizationScript.py

}

}

However, my build fails with:

execCommand == null!

Is there a way to specify that the do last block is the piece that will be executing said script?

I’ve found a solution but it feels more like a workaround that a real fix.

task generateLocalizedStrings(type:Exec) {
        ext.srcFile = file('../../../localization/language-files/en_US/wdmobilestringres.properties')
        ext.destDir = new File("src/main/res")
        inputs.file srcFile
        outputs.dir destDir
          workingDir '../../../localization/'
        commandLine 'python', 'dummyScript.py'
          doLast {
            println "Executing localization script"
            workingDir '../../../localization/'
            commandLine 'python', 'localizationScript.py'
        }
    }

Well apparently the doLast block isn’t needed for gradle to prevent the script from running. So the solution is to just remove the doLast block.

doLast is for adding commands to the execution phase of the task. It does not change the point at which the task is executed. When using builtin tasks, you normally would not need a doLast block. The task execution order is controlled by the graph of task dependencies (i.e. dependsOn).

I ran into the same issue trying to use the Exec task with runtime properties. These threads offer some insight and an approach that worked for me: http://forums.gradle.org/gradle/topics/why_does_a_type_exec_task_require_that_one_not_use http://forums.gradle.org/gradle/topics/exec_task_dependent_on_execution_phase_task