简体   繁体   English

如何使用任务 gradle 删除文件

[英]How to remove file using a task gradle

I'm trying to remove a file using a gradle task我正在尝试使用 gradle 任务删除文件

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

and I got error "A problem occurred starting process 'command 'rm'"我收到错误消息“启动进程‘command‘rm’时出现问题”

Note: I don't want to use task something(type: Delete) and no external scripts.注意:我不想使用task something(type: Delete)并且没有外部脚本。 it must be from commandLine.它必须来自命令行。 There is a possibility?有可能吗?

I'm trying to remove a file using a gradle task我正在尝试使用 gradle 任务删除文件

Gradle provides both a task type ( Delete ) and a method ( delete ) for this use case. Gradle 为该用例提供了任务类型 ( Delete ) 和方法 ( delete )。

Note: I don't want to use task something(type: Delete)注意:我不想使用task something(type: Delete)

Why?为什么? If you don't want to use Gradle functionality, then don't use Gradle at all.如果您不想使用 Gradle 功能,则根本不要使用 Gradle。

it must be from commandLine.它必须来自命令行。

As others have mentioned, the command rm is not available on Windows systems.正如其他人提到的,命令rm在 Windows 系统上不可用。 This is one of the reasons why this functionality gets abstracted by Gradle.这是此功能被 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 任务,因为它不会执行任何功能。 Gradle distinguishes between the configuration phase and the execution phase . Gradle区分配置阶段执行阶段 The <code> inside the closure of a task definition task <name> { <code> } will be executed directly (during configuration phase).任务定义task <name> { <code> } <code>包内的 <code> 将直接执行(在配置阶段)。 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).只有(内部)任务操作、 doFirstdoLast闭包在实际任务执行时执行(从命令行或作为任务依赖项)。

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:要解决这个问题,要么直接使用Delete类型的任务(无论如何你都应该这样做),要么使用Exec类型的任务,要么将exec方法包装在doFirst / doLast闭包中:

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"如果您使用的是 Windows,则应使用命令“del /q”而不是“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.正如我在评论中提到的,我建议使用 Groovy 删除文件,因为从命令行执行特定于环境的命令我们正在失去互操作性,因此当您将环境切换到 Mac 或 Linux 时,该命令将不再起作用。 Also try maybe with this piece of code which I've got from this SO post: Groovy executing shell commands也可以尝试使用我从这篇 SO 帖子中获得的这段代码: Groovy 执行 shell 命令

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:使用普通的 groovy 方法应该非常简单:

task remove { 
    String filename = "myFile.txt"  
    boolean fileSuccessfullyDeleted =  new File(filename).delete()  
    println fileSuccessfullyDeleted 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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