-2

I'm trying to remove a file using a gradle task

task remove {
    exec {
        working dir file/file2
        commandLine 'myFirstCommandLineWhichWorks'
        commandLine "rm", "'myFile.txt'"
    }
}

and I got error "A problem occurred starting process 'command 'rm'"

Note: I don't want to use task something(type: Delete) and no external scripts. it must be from commandLine. There is a possibility ?

Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
discCard
  • 421
  • 1
  • 5
  • 16

2 Answers2

2

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'
        }
    }
}
Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
0

If you are using Windows you should use command "del /q" instead of "rm"

task remove {

   exec {

   working dir file/file2
   commandLine 'myFirstCommandLineWhichWorks'
   commandLine "del /q", "'myFile.txt'"

   }

}

As I mentioned in comments, I would suggest to use Groovy for deleting files, because executing environment specific commands from command line we are loosing the interoperability, so when you switch the environment to Mac or Linux the command won't work anymore. Also try maybe with this piece of code which I've got from this SO post: Groovy executing shell commands

task remove {

    def sout = new StringBuilder(), serr = new StringBuilder()
    def proc = 'del myFile.txt /q'.execute()
    proc.consumeProcessOutput(sout, serr)
    proc.waitForOrKill(1000)
    println "out> $sout err> $serr"

}

And with plain groovy method it should be super simple:

task remove { 
    String filename = "myFile.txt"  
    boolean fileSuccessfullyDeleted =  new File(filename).delete()  
    println fileSuccessfullyDeleted 
}
Mariusz Brona
  • 1,549
  • 10
  • 12
  • it doesn't work, I got "A problem occurred starting process 'command 'del /q" – discCard Oct 26 '20 at 08:23
  • 1
    Why can't you just use Groovy to actually just delete the file? Why it has to be the command line? – Mariusz Brona Oct 26 '20 at 08:25
  • Might have to be `"del", "/q", "'myFile.txt'"` – Zoe Oct 26 '20 at 08:27
  • Yeah, or even `"del", "'myFile.txt'", "/q"` – Mariusz Brona Oct 26 '20 at 08:29
  • Mainly meant that I'm not entirely sure Groovy is fine with a space in there, and therefore surrounds it in quotes, making the terminal interpret "del /q" as an executable rather than del with a parameter /q. I have no idea though, I'm by no means this familiar with Groovy, nor have I ever used `commandLine` in Groovy before. – Zoe Oct 26 '20 at 08:32
  • Now I got `A problem occurred starting process 'command 'del"` – discCard Oct 26 '20 at 08:34
  • 1
    @Zoe I mean by using the command line directly from groovy code we loose all of the interoperability, so when you execute this Windows code on Linux or Mac it won't work anymore. And with plain Groovy it should work in all environments – Mariusz Brona Oct 26 '20 at 08:34
  • and what would the removal using groovy look like? – discCard Oct 26 '20 at 08:46
  • but now using groovy, how it will be know where my files is ? what path to this file ? – discCard Oct 26 '20 at 08:53
  • 1
    So pass the filePath via the argument – Mariusz Brona Oct 26 '20 at 09:07
  • Each task definition in this answer is completely irrelevant, because their functionality is executed during *configuration phase*. The file will be deleted each time `gradle` is invoked (with any task names as arguments). – Lukas Körfer Oct 26 '20 at 11:20