Gradle Task Execution在doFirst中的commandLine之后跳过

时间:2016-04-15 08:00:33

标签: windows gradle

这是Windows上的构建过程。 我有gradle任务(类型:Exec),它使用其 doFirst 闭包来临时复制外部目录中的文件,这是其构建过程所必需的:

task(myTask, type: Exec) {
    [...]
    < do own stuff >
    [...]

    doFirst {
        println "Copy file"
        if (System.properties['os.name'].toLowerCase().contains('windows')) {
            commandLine 'cmd', '/c', "copy .\\<myFile> .\\..\\.."
        }
    }
}

出于某种原因,在 doFirst 中使用 commandLine 来执行复制命令,使整个任务终止并阻止执行任务的主要目的

使用switch - debug 我查看了调试输出,看到copy命令使状态更改为成功

[DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTING
[DEBUG] [org.gradle.process.internal.DefaultExecHandle] Waiting until process started: command 'cmd'.
[DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTED
[DEBUG] [org.gradle.process.internal.ExecHandleRunner] waiting until streams are handled...
[INFO] [org.gradle.process.internal.DefaultExecHandle] Successfully started process 'command 'cmd''
[QUIET] [system.out]         1 file(s) copied.
[DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: SUCCEEDED
[DEBUG] [org.gradle.process.internal.DefaultExecHandle] Process 'command 'cmd'' finished with exit value 0 (state: SUCCEEDED)
[DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task 'myTask'

如何防止这种情况&#34; commandLine&#34;在 doFirst 中停止任务进一步执行?

1 个答案:

答案 0 :(得分:0)

而不是commandLine封闭中的doFirst只需使用普通的旧copy块:

task(myTask, type: Exec) {
    [...]
    < do own stuff >
    [...]

    doFirst {
        println "Copy file"
        copy {
           from "some file"
           into "some path"
        }
    }
}