2

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.

iggy
  • 1,613
  • 1
  • 18
  • 35
  • That is what it looks like to me, but I don't know much about this. – Evan Carslake Jul 13 '15 at 18:47
  • Without the closure, what happens when you execute `gradle hello` ? – jaco0646 Jul 13 '15 at 18:50
  • @jaco0646 you are calling the task directly. I need it to be called during execution phase and not configuration phase – iggy Jul 13 '15 at 18:56
  • Calling the task directly is the execution phase: https://docs.gradle.org/current/userguide/build_lifecycle.html. – jaco0646 Jul 13 '15 at 19:01
  • @jaco0646 what if this task is a dependency of another one? – iggy Jul 13 '15 at 19:22
  • `task anotherOne(dependsOn : hello)` https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies – jaco0646 Jul 13 '15 at 19:28
  • @jaco0646 I mean will it be called during execution phase if it is a dependency of another task? The question is "how to make the task be called during execution in general" – iggy Jul 13 '15 at 21:02
  • you can just call it from the commandline: "gradle hello". otherwise you should declare a dependency on this task from one of your other tasks you have in your build. To help you best, can you elaborate a bit about your usecase here? – Rene Groeschke Jul 13 '15 at 21:52
  • @ReneGroeschke please see the edit. I am quite new to gradle so I am not sure what is important to specify and what not. – iggy Jul 13 '15 at 22:05
  • 1
    @iggy why do you think the task is executed during configuration phase? – Rene Groeschke Jul 13 '15 at 22:06
  • @ReneGroeschke I guess I am not exactly sure how execution phase works when we don't say `doLast` or `<< {}` for a task – iggy Jul 13 '15 at 22:22
  • @ReneGroeschke I would really appreciate if you could elaborate how this works or maybe point me to a link that would explain this mechanism – iggy Jul 13 '15 at 22:26
  • 1
    @iggy your question contains a link explaining that mechanism. It says: *Gradle will call the method when the task executes*. Note the wording. It says "when the task executes". Not "when the task is configured". So the method annotated with `@TaskAction` is what the task does, during the execution phase. It also says: *You don't have to use a method to define the behaviour for the task. You could, for instance, call doFirst() or doLast() with a closure in the task constructor to add behaviour*. So this TaskAction-based mechanism is just another way of telling the task what to do when executed – JB Nizet Jul 14 '15 at 07:14
  • @JBNizet explained it very well. To understand the phases of a gradle build I recommend you the according gradle user guide section at https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:build_phases – Rene Groeschke Jul 14 '15 at 08:38
  • @JBNizet thanks guys, I now understand how it works. – iggy Jul 17 '15 at 08:37

1 Answers1

5

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.

Peter Ledbrook
  • 4,302
  • 1
  • 21
  • 18
  • thanks for explaining, I had some gaps in understanding how configuration phase in gradle works – iggy Jul 17 '15 at 08:36