简体   繁体   中英

How to remove file using a task gradle

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?

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

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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM