gradle 自定义任务执行阶段

igg*_*ggy 2 gradle

这个问题是针对 gradle (>= 2.4)。我想编写一个如下的自定义任务:

https://docs.gradle.org/current/userguide/custom_tasks.html

class GreetingTask extends DefaultTask {
    @TaskAction
    def greet() {
        println 'hello from GreetingTask'
    }
}

task hello(type: GreetingTask)
Run Code Online (Sandbox Code Playgroud)

如何在执行阶段运行此任务?正在传递一个空的闭包

<< {

}
Run Code Online (Sandbox Code Playgroud)

唯一的解决办法?

编辑

该任务应该用于具有多个任务作为依赖项的多项目构建。

我希望该命令gradle build可以通过说类似的话来构建所有项目

`build.dependsOn(hello)`
Run Code Online (Sandbox Code Playgroud)

但似乎任务 hello 在构建的配置阶段被调用。

Pet*_*ook 5

将以下内容添加到build.gradle文件中:

class GreetingTask extends DefaultTask {
    @TaskAction
    def greet() {
        println 'hello from GreetingTask'
    }
}

task hello(type: GreetingTask) {
    println "This is the configuration phase"

    doFirst {
        println "This is the execution phase"
    }
}
Run Code Online (Sandbox Code Playgroud)

现在执行gradle hello。您将看到的输出是

This is the configuration phase
:hello
This is the execution phase
hello from GreetingTask

BUILD SUCCESSFUL
Run Code Online (Sandbox Code Playgroud)

如您所见,任务的输出发生在 之后doFirst(),这肯定发生在执行阶段。