Gradle series 10-gradle life cycle hook method introduction

Gradle project build life cycle

The life cycle of Gradle is divided into three phases, initialization phase, configuration phase, and execution phase.

Initialization phase

Determine which projects need to be initialized by setting.gradle, load the build.gradle files of all projects that need to be initialized, and create Project objects for each project

Configuration phase

Execute the build.gradle script under each project, complete the project configuration, and construct the task task dependency graph to execute the code in the Task according to the dependency during the execution phase

Configuration code: the code that needs to be executed during the configuration phase

task configCode{
	println 'config code'
}
Execution phase

Through the Task dependency graph in the configuration phase, the action code in the Task is executed in order, that is, the code written in doFirst and doLast in the Task is executed. The
action code: the code that will be executed only when the task is called

	task executeCode {
		doLast{
			println 'execute code'
		}
	}

The life cycle & hook method diagram is as follows:
Insert picture description here

Demo of code used by hook method

Open the setting.gradle file and add the following code:

gradle.settingsEvaluated {
    println 'init phase: settingsEvaluated'
}

gradle.projectsLoaded {
    println 'init phase: projectsLoaded'
}

gradle.beforeProject {
    println 'init phase: projectsLoaded'
}

After adding the effect diagram display:
Insert picture description here
add the following hook method in build.gradle:

task t1 {
    //配置代码
    println 't1 configuration'

    //动作代码
    doFirst {
        println 't1 execute doFirst'
    }
    doLast {
        println 't1 execute doLast'
    }
}

gradle.afterProject {
    println 'config phase: afterProject'
}

gradle.projectsEvaluated {
    println 'config phase: projectEvaluated'
}

//任务图
gradle.taskGraph.whenReady {
    println 'config phase: taskGraph.whenReady'
}

gradle.taskGraph.beforeTask{
    println 'execute phase: taskGraph.beforeTask'
}

gradle.taskGraph.afterTask {
    println "execute phase: taskGraph.afterTask"
}

gradle.buildFinished {
    println 'execute phase: buildFinished'
}

Execute gralde build, the results are as follows

20:20:11: Executing task 'build'...

init phase: settingsEvaluated
init phase: projectsLoaded

> Configure project :
init phase: projectsLoaded
t1 configuration
config phase: afterProject
config phase: projectEvaluated
config phase: taskGraph.whenReady

> Task :compileJava UP-TO-DATE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :processResources NO-SOURCE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :classes UP-TO-DATE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :jar UP-TO-DATE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :assemble UP-TO-DATE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :compileTestJava NO-SOURCE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :processTestResources NO-SOURCE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :testClasses UP-TO-DATE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :test NO-SOURCE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :check UP-TO-DATE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask

> Task :build UP-TO-DATE
execute phase: taskGraph.beforeTask
execute phase: taskGraph.afterTask
execute phase: buildFinished

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 up-to-date
20:20:11: Task execution finished 'build'.

We can see that the configuration code log has been output

t1 configuration

But the action code (doFirst & doLast) has not been executed. We have already understood this result in the experiment before. Only the build is actually similar to the compilation in java. There is no actual task to call t1. Below we double-click t1 under other to build and call the t1 task , The output is as follows:

20:46:12: Executing task 't1'...

init phase: settingsEvaluated
init phase: projectsLoaded

> Configure project :
init phase: projectsLoaded
t1 configuration
config phase: afterProject
config phase: projectEvaluated
config phase: taskGraph.whenReady

> Task :t1
execute phase: taskGraph.beforeTask
t1 execute doFirst
t1 execute doLast
execute phase: taskGraph.afterTask
execute phase: buildFinished

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
20:46:14: Task execution finished 't1'.

Here we can clearly see the execution order of the hook methods in the life cycle

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

Guess you like

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