Gradle Tips #1-tasks,
Original article link
Starting with this blog, I will write a series of articles about Gradle to record Gradle that I understand since I came into contact with the Gradle build script.
Today we will talk about the configuration and running of Gradle tasks and tasks. Some readers may not understand Gradle tasks, so it is easier to use real examples to show them. The following code shows three Gradle tasks, which will be explained later.
task myTask { println "Hello, World!" } task myTask { doLast { println "Hello, World!" } } task myTask << { println "Hello, World!" }
My goal is to create a task that prints "Hello, World!" When executed !". When I create a task for the first time, I guess it should be written like this:
task myTask { println "Hello, World!" }
Now, try to execute this myTask, input gradle myTask in the command line, and print as follows:
user$ gradle myTask Hello, World! :myTask UP-TO-DATE
This task seems to work. It prints "Hello, World !".
However, it is not as expected. Let's see why. Enter gradle tasks in the command line to view all available tasks.
user$ gradle tasks Hello, World! :tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] ..........
Why? "Hello, World !" Printed? I just want to see which tasks are available and do not execute any custom tasks!
The reason is actually very simple. Gradle task has two main stages in its lifecycle: configuration stage and execution stage.
Maybe my words are not accurate, but it does help me understand tasks.
Before executing a task, Gradle must first configure the task. Then the problem arises. How do I know which code is executed in the configuration process and which code is executed in the task? The answer is that the Code at the top layer of the task is the configuration code, for example:
Task myTask {def name = "Pavel" // <-- this line of code runs println "Hello, World! "/// <-- This line of code will also be executed in the configuration phase}
This is why "Hello, World!" is printed when I execute gradle tasks !" -The configuration code is executed. But this is not what I want. I want "Hello, World !" It is printed only when myTask is explicitly called. To achieve this, the simplest method is to use the Task # doLast () method.
task myTask { def text = 'Hello, World!' //configure my task doLast { println text //this is executed when my task is called } }
Now, "Hello, World !" It is printed only when I execute gradle myTask. Cool, now I know how to configure and make the task do the right thing. In the first example, what does the <symbol of the third task mean?
task myTask2 << { println "Hello, World!" }
This is actually a syntactic sugar version of doLast. It works the same as the following statement:
task myTask { doLast { println 'Hello, World!' //this is executed when my task is called } }
However, in this way, all the code is executed and no part of the code is configured. Therefore, it is suitable for tasks that are simple and do not need to be configured. Once your task needs to be configured, you still need to use the doLast version.
Happy Gradling