iggy
iggy

Reputation: 1743

gradle custom task execution phase

This question is for gradle (>= 2.4). I would like to write a custom task like the following:

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

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

task hello(type: GreetingTask)

how can I make this task run during execution phase? Is passing an empty closure with

<< {

}

the only solution?

Edit

the task is supposed to be used in a multiproject build with several tasks as dependencies.

I'd like that the command gradle build would build all the projects by saying something like

`build.dependsOn(hello)`

but seems that the task hello is called during configuration phase of the build.

Upvotes: 2

Views: 1727

Answers (1)

Peter Ledbrook
Peter Ledbrook

Reputation: 4492

Add the following to a build.gradle file:

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

Now execute gradle hello. The output you will see is

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

BUILD SUCCESSFUL

As you can see, the output from the task occurs after the doFirst(), which definitely happens during the execution phase.

Upvotes: 5

Related Questions