7 Detailed Gradle build the project life cycle

Previous We already know how to customize the task, but also know to perform tasks only need to click on the next task on the ok, and write directly in the task code will be executed while performing any task. So why the code is executed? The order of execution is what is it? Take a look at Gradle build lifecycle to clear.

Gradle build the project life cycle

Gradle life cycle in three stages: the initialization phase, the configuration phase, implementation phase. That these three stages in doing things?

Initialization phase

By settings.gradle determine which projects need to initialize, load all need to initialize the project build.gradle files and create project object for each project.

Configuration phase

Build.gradle script execution under each line, to complete the project configuration and configured Task task dependency graph in order to perform in accordance with the configuration code Task dependencies in the execution stage.

Configuration code : code configured to be executed on the stage, as follows:

task configCode{
   println 'config Code'
}

The implementation phase

By Task diagram of the configuration phase, in order to perform tasks you need to perform an action code in, writing code is the implementation of the mandate of the doFirst or doLast.

Action code : task calling code will be executed.

task executeCode << {
   println 'execute Code'
}

These are the life cycle of Gradle build the project, due to the powerful and easy distribution of Gradle, if you want to do some extra work during the construction of Gradle, then you can use the built-in hook method. Let's Gradle the life cycle and the hook by a method described in FIG.

Gradle life cycle and hook method

807144-bab959dab5b4ef8e
Gradle life cycle and hook method

The chart above shows the entire construction process Gradle, there are many ways for the user to hook themselves use to cover the three stages of the life cycle.

note:

  1. Hook method initialization phase and gradle.beforeProject () can only be defined in setting.gradle or init.gradle script.
    Project has been the object execution build.gradle. And before executing it called beforeProject hook method.
  2. gradle.buildStarted () hook method to not be executed.
    By that source, before it has been called buildStarted initialization method, so the initialization phase can not callback to.

example

setting.gradle

rootProject.name = 'gradleWeb'

// 初始化阶段
gradle.settingsEvaluated {
    println '初始化阶段settingsEvaluated'

}
gradle.projectsLoaded  {
    println '初始化阶段 projectsLoaded'
}

// 配置阶段
gradle.beforeProject {
    println '配置阶段 beforeProject'
}

build.gradle

// 自定义任务
task t1 {
    println 't1 configuration'
    doLast {
        println 't1 execute doLast'
    }
    doFirst {
        println 't1 execute doFirst'
    }
}
// 钩子方法
gradle.afterProject {
    println '配置阶段 afterProject'
}
project.beforeEvaluate {
    println '配置阶段 beforeEvaluate'
}
gradle.projectsEvaluated {
    println '配置阶段 projectsEvaluated'
}
project.afterEvaluate {
    println '配置阶段 afterEvaluate'
}
gradle.taskGraph.whenReady {
    println '配置任务 whenReady'
}

// 执行阶段
gradle.taskGraph.beforeTask {
    println "执行阶段 before task"
}
gradle.taskGraph.afterTask {
    println "执行阶段 afterTask "
}
gradle.buildFinished {
    println '构建结束 buildFinished'
}

Execution gradle t1results are as follows:

初始化阶段settingsEvaluated
初始化阶段 projectsLoaded

> Configure project :gradleWeb
15:16:02: Executing task 't1'...

初始化阶段 settingsEvaluated
初始化阶段 projectsLoaded

> Configure project :
配置阶段 beforeProject
t1 configuration
配置阶段 afterProject
配置阶段 afterEvaluate
配置阶段 projectsEvaluated
配置任务 whenReady

> Task :t1
执行阶段 before task
t1 execute doFirst
t1 execute doLast
执行阶段 afterTask 

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
构建结束 buildFinished
15:16:03: Task execution finished 't1'.

These are the introduction Gradle build the project life cycle and hook methods, these methods typically use less hooks, it can be used under special circumstances demand.
Understanding of the life cycle Let's have a deeper understanding of Gradle. Gradle course, there are many points we deserve to learn and study. For example, a more important point: dependency management. Next: "Gradle project dependency management."

Reprinted link: https://www.jianshu.com/p/a132f6a77e7a

Guess you like

Origin blog.csdn.net/weixin_34023863/article/details/91014754