Gradle-define Task and Gradle-define Task

Source: Internet
Author: User

Gradle-define Task and Gradle-define Task

We talked about the Groovy syntax and the execution sequence of Gradle. This article describes the definition of a Task.

Task can be understood as the execution unit of Gradle, which is too important. According to the previous analysis, Gradle completes the specific build tasks through tasks. Let's take a look at the definition of tasks.

The simplest and most straightforward method is defined as follows:

task myTask {    println "config myTask"}

Run

renyugang$ gradle myTaskconfig myTask

Then execute

renyugang$ gradle cleanconfig myTask

We can see that the code inside the brackets is executed in the configuration phase for the task defined in the preceding method. That is to say, as long as I execute any task, the code will be executed, this is because the complete configuration is required before each task is executed.

However, in many cases, we do not need to write the configuration code. We want the code in the brackets to be executed only when executing our task. This can be done through doFirst or doLast.

DoFirst: the initial operation doLast: the last operation when the task is executed.
// Define and configure myTasktask myTask {println "config myTask"} myTask. doLast {println "after execute myTask"} myTask. doFirst {println "before execute myTask "}

The execution result is as follows:

config myTask:app:myTaskbefore execute myTaskafter execute myTask

In addition, doLast also has an equivalent operation leftShift, which can also be abbreviated as <, so the following three implementations are equivalent:

myTask.doLast {    println "after execute myTask"}myTask.leftShift {    println "after execute myTask"}myTask << {    println "after execute myTask"}

In the demo above, myTask did not do anything when it was actually executed. It just did some things at the beginning and end of the execution.

In fact, the @ TaskAction operator can also be used to specify the tasks to be executed by a Task, which is different from doFirst and doLast. However, @ TaskAction is rarely used at ordinary times. Here is a simple example:

Class RygTask extends DefaultTask {String message = 'this is rygtask' // @ TaskAction indicates the action to be executed by the Task, that is, when the Task is called, hello () the @ TaskAction def hello () {println "Hello world. $ message "}}// hello uses the default message value task hello (type: RygTask) // reset the message value task hello1 (type: RygTask) {message = "I am an android developer "}

Rungradle hello hello1

:app:helloHello world. This is RygTask:app:hello1Hello world. I am an android developer

In addition to the preceding Task definition method, Gradle also provides some existing tasks, such as Copy, Delete, and Sync. Therefore, when defining a Task, we can inherit the existing Task. For example, we can inherit the system Copy Task to complete the file Copy operation.

task myTask(type: Copy) { configure closure }

In addition to this method, we can also use APIs to dynamically create tasks. There are also a lot of APIs. Here we will introduce several of the most commonly used (Gradle water is very deep, and APIs are particularly flexible, remember the most common ones ).

// The following three definitions are exactly the same: task myTask <{println "after execute myTask"} project. task ('mytask '). doLast {println "after execute myTask"} project. tasks. create ('mytask '). doLast {println "after execute myTask "}

When defining a Task, you can specify many parameters as follows:

Parameters Description Default Value
name Task Name Cannot be blank. You must specify
type Parent class of a task" DefaultTask
overwrite Whether to replace an existing task false
dependsOn Set of tasks on which the task depends []
group Group of tasks null
description Task Description null

Parameters are not involved in the preceding example. The following example shows the example with parameters.

Task myTask1 <{println "execute myTask1"} task myTask2 <{println "execute myTask2"} // defines a task named rygTask, which belongs to the renyugang group, and dependent on myTask1 and myTask2. Project. task ('rygtask', group: "renyugang", description: "My own Task", dependsOn: ["myTask1", "myTask2"]). doLast {println "execute rygTask "}

PassGradle tasks:

Publishing tasks publish-Publishes artifacts to submit. Renyugang tasks unzip rygTask-my own TaskUpload tasks ---------- uploadArchives-Uploads all artifacts belonging to configuration ': AndroidStub: archives'

Try to executeGradle rygTask:

:app:myTask1execute myTask1:app:myTask2execute myTask2:app:rygTaskexecute rygTask

I don't need to explain the results. I believe everyone can understand them.

Additional instructions

The Gradle command supports spelling together, for exampleGradle hello hello1, then gradle will first execute hello, and then execute hello1. The Gradle command can be abbreviated only if it can uniquely limit a task. For example, rygTask can be abbreviatedgradle rTask。

Finally, there are a lot of APIS for defining tasks. I have introduced the most commonly used parts. For the remaining details, you still need to check the Gradle document. In fact, learning Gradle is a document Query Process. Read the following documents.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.