什么时候doFirst在gradle中执行

时间:2016-07-21 02:16:23

标签: gradle

为什么0000在gradle foo之后不退出?删除doFirst foo.doFirst{ delete '0000' }后,0000就在那里。

应该在foo任务脚本之前执行

doFirst

文档说:

Task doFirst(Closure action)

Adds the given closure to the beginning of this task's action list. The closure is passed this task as a parameter when executed.

完整命令日志:

isaac-mbp:swap isaac$ cat build.gradle
task foo {
    mkdir '0000/a'
    mkdir '0000/b'
}
foo.doFirst{ delete '0000' }

isaac-mbp:swap isaac$ gradle foo
:foo

BUILD SUCCESSFUL

Total time: 0.581 secs
isaac-mbp:swap isaac$ ls -lha 0000
ls: cannot access 0000: No such file or directory
isaac-mbp:swap isaac$ gradle -v

------------------------------------------------------------
Gradle 2.14
------------------------------------------------------------

Build time:   2016-06-14 07:16:37 UTC
Revision:     cba5fea19f1e0c6a00cc904828a6ec4e11739abc

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_25 (Oracle Corporation 25.25-b02)
OS:           Mac OS X 10.11.5 x86_64

1 个答案:

答案 0 :(得分:2)

没有。

  1. 任务是(简化)按照添加顺序执行的操作集合
  2. build.gradle分两个阶段运行:配置执行
  3. 以下代码中的逻辑(mkdir s)将被执行每个时间build.gradle被评估,因为它已添加到配置中时间:

    task foo {
        mkdir '0000/a'
        mkdir '0000/b'
    }
    

    要解决此问题 - 添加操作:

    task foo << {
        mkdir '0000/a'
        mkdir '0000/b'
    }
    

    现在doFirst

    task foo << {
            mkdir '0000/a'
            mkdir '0000/b'
    }
    foo.doFirst { delete '0000' }
    

    现在该任务有两个操作:mkdirsdelete,它们将以适当的顺序执行。请查看here