使用doLast的Gradle exec任务失败

Zac*_*ske 7 gradle

我正在尝试仅在自上次构建后更新文件时才运行exec任务.我最初的尝试看起来像这样:

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'
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这失败了"execCommand == null!"

我找到了一个解决方法,但我真的更喜欢一个合适的解决方案.

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'
    }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*now 12

一种选择是使任务成为通用任务并显式调用execdsl.例如:

task("MyTask", type: Exec) {

    doLast {
        commandLine "your commandline"
    }
}
Run Code Online (Sandbox Code Playgroud)

对此

task("MyTask") {

    doLast {
        exec {
            commandLine "your commandline"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Pet*_*ser 3

您需要摆脱该doLast块,并在该块之外进行workingDir配置commandLine。(在任务运行后配置任务就太晚了。)