Introduction to the Gradle series 8-gradle build script

Build script summary

The two most important concepts in the Gradle build script are project and Task . Any Gradle build is composed of one or more projects. Each project includes many building parts, which can be a jar package or a web application. It can also be the integration of multiple jar packages, which can deploy applications and build environments.
Each project is composed of one or more tasks, and each Task represents an atomic operation during construction execution. Such as compiling, packaging, generating JavaDoc, publishing to the warehouse, etc.
The relationship between project and project, project and Task, and task and task is shown in the following figure:
Insert picture description here
Project1 depends on Project2, so you need to build Project2 first, and there are three Task FGH in Project2. Execute TaskH, the Task in Project1 is also executed according to this dependency order

Introduction to project objects

A project represents a component (jar / war file) being built. When the build begins, Gradle will instantiate an org.gradle.api.Project object based on build.gradle and implicitly call its members through the project variable.
Insert picture description here
Other common configuration of Project:

  1. plugins apply plugin is used to introduce plugins
  2. dependencies
  3. repositories warehouse configuration
  4. task task
  5. ext.gradle.properties add additional properties

All configuration will be encapsulated in the Project object, and then called by the project instance in the property

Task task introduction

Each task will be encapsulated into an org.gradle.api.Task object during construction and execution, mainly including task actions and task dependencies. Task actions define an atomic operation, which can be defined to depend on other tasks, action sequences, and execution conditions.

The main operation actions of the task:
dependsOn: dependent related operations
doFirst: method before task execution
doLast, <<: method after task execution

Writing actual combat tasks

Based on the gradle project created in the previous section, we write directly in the build.gradle file, the
Insert picture description here
code is as follows:

//演示任务的书写
task t1 {
    println "hello t1"
}

task t2(dependsOn: 't1') {
    //t2执行之前的操作
    doFirst {
        println  't2 do first'
    }

    println 'hello t2'

    //t2执行之后的操作
    doLast {
        println 't2 do last'
    }
}

After writing the task, we try to execute it. First, click the refresh button.
Insert picture description here
We double-click the build to see the execution result.

16:51:35: Executing task 'build'...


> Configure project :
hello t1
hello t2

> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar UP-TO-DATE
> Task :assemble UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build UP-TO-DATE

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 up-to-date
16:51:36: Task execution finished 'build'.

From the output, we can see that the construction of the project only performed the operations inside the task, and did not perform doFirst and doLast

We double-click t1 under other to see the execution result:

16:55:00: Executing task 't1'...
> Configure project :
hello t1
hello t2
> Task :t1 UP-TO-DATE
BUILD SUCCESSFUL in 0s
16:55:01: Task execution finished 't1'.

We double-click t2 under other to see the execution result:

16:54:04: Executing task 't2'...
> Configure project :
hello t1
hello t2
> Task :t1 UP-TO-DATE
> Task :t2
t2 do first
t2 do last
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
16:54:04: Task execution finished 't2'.

According to the output, where

> Configure project :
hello t1
hello t2

This log is the output of building the project, and the project will be configured first when performing the task

Task :t2
t2 do first
t2 do last

When executing the t2 task, doFirst and doLast executed

From this experiment, we can conclude that
the code directly defined under the task will be executed when the project is configured, and will not be executed at other times, even if it is dependent. Only the operations in doFirst and doLast will be called when the task is called or when it depends on execution. Therefore, the code of the custom task needs to be written in doFirst or doLast, unless it is executed when the Project is now built.

Published 159 original articles · 22 praises · 90,000+ views

Guess you like

Origin blog.csdn.net/ytuglt/article/details/104984616