I'm trying to remove a file using a gradle task
Gradle provides both a task type (Delete
) and a method (delete
) for this use case.
Note: I don't want to use task something(type: Delete)
Why? If you don't want to use Gradle functionality, then don't use Gradle at all.
it must be from commandLine.
As others have mentioned, the command rm
is not available on Windows systems. This is one of the reasons why this functionality gets abstracted by Gradle.
There is another problem with your code: You define a Gradle task that is completely irrelevant, as it won't execute any functionality. Gradle distinguishes between the configuration phase and the execution phase. The <code>
inside the closure of a task definition task <name> { <code> }
will be executed directly (during configuration phase). Only (internal) task actions, doFirst
and doLast
closures are executed when the actual task is executed (either from command line or as a task dependency).
To solve this problem, either use a task of type Delete
directly (you should do this anyway), use a task of type Exec
or wrap the exec
method in a doFirst
/ doLast
closure:
task remove(type: Delete) {
delete 'myFile.txt'
}
task remove(type: Exec) {
commandLine 'del', 'myFile.txt', '/q'
}
task remove {
doFirst {
exec {
commandLine 'del", 'myFile.txt', '/q'
}
}
}