Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle exec task with doLast fails

Tags:

gradle

I'm trying to run an exec task only if a file has been updated since the last build. My initial attempt looked like this:

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 {
        println "Executing localization script"
        workingDir '../../../localization/'
        commandLine 'python', 'localizationScript.py'
    }
}

However, this fails with "execCommand == null!"

I found a workaround but I'd really prefer a proper solution.

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'
    }
}
like image 897
Zach Sperske Avatar asked Jan 13 '15 19:01

Zach Sperske


2 Answers

One option is to make the task a generic task and to explicitly call the exec dsl. E.g. from this:

task("MyTask", type: Exec) {

    doLast {
        commandLine "your commandline"
    }
}

to this

task("MyTask") {

    doLast {
        exec {
            commandLine "your commandline"
        }
    }
}
like image 152
Chris Snow Avatar answered Nov 14 '22 00:11

Chris Snow


You need to get rid of the doLast block, and instead configure workingDir and commandLine outside that block. (Configuring a task after it has run is too late.)

like image 40
Peter Niederwieser Avatar answered Nov 14 '22 00:11

Peter Niederwieser