Windwalker
Windwalker

Reputation: 1945

Gradle Task Execution Skipped after commandLine in doFirst

This is a build process on Windows. I have gradle task (type: Exec) which uses its doFirst closure to temporarily copy a file from a foreign directory, which is required for its build process:

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

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

For some reason, using commandLine within doFirst, to execute the copy command, makes the whole task terminate and prevents the execution of the task's main purpose.

Using switch --debug I took a look into the debug output and saw that the copy command makes the state to be changed to succeeded:

[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'

How can I prevent this "commandLine" within doFirst to stop the task further execution?

Upvotes: 0

Views: 581

Answers (1)

Opal
Opal

Reputation: 84786

Instead of commandLine in doFirst closure just use plain old copy block:

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

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

Upvotes: 0

Related Questions