Two important concepts
project
In fact, a project depends on what you use Gradle what to do? Construction of the project usually represents the content. For example, in Android, a module is a project;
- Project is registered in the setting.gradle
- Usually a project has a build.gradle
Gradle is constructed by one or more of the project components.
task
The name suggests is a task in the construction phase of the operation to be performed. It is an atomic unit of work Gradle build. E.g. compile Java source code;
Task is defined in the build script project, and can rely on each other.
A project is composed of a task.
Each construct will Gradle through three distinct stages in the same sequence:
initialization
Gradle supports single and multi-project build project build.
At this stage Gradle will identify which projects will participate in the construction. Gradle will be determined by setting.gradle single project or multi-project build.
Gradle creates a Project instance for each project.
Configuration
In this configuration script execution phase determined in the initialization phase of each project, but does not perform the task which will only assess task dependencies, create tasks according to their dependence directed acyclic graph.
Gradle introduced with the demand characteristics of a variable called configuration, the characteristic that it can be arranged only relevant and necessary items during the build process. This is useful in large multi-project build, as it can greatly reduce build times.
carried out
At this stage, Gradle will identify the tasks created in the configuration phase of the directed acyclic graph. And in accordance with their dependency order started.
All building work is carried out at this stage. Such as compiling source code to generate .class files, copy files.
setting.gradle
This file is named by the Gradle agreement, default name setting.gradle, to be executed during the initialization phase.
For multi-project build, in a statement here must be involved in all construction projects. For a single project build it is optional, and dispensable.
How Gradle is looking setting.gradle of?
- Looking in the current directory
- Not found, then go looking for parent directory
- I am still not found a single project to build a
- If you find one of the project is to determine if the project is currently executing setting.gradle is defined on the implementation of multi-project build, or to perform a single project build.
Property access and method calls a script is entrusted to the instance of the Project class, property access and method calls are similar setting.gradle entrusted to the instance of the class of objects Settings.
Construction of single project
For a single project build, very simple workflow after initialization, project build script objects created for initialization phases. Find the same tasks at the command line passed the task name.
If the task is present as a separate construct in accordance with the order of execution of the command transfer.
Construction of multi-project
Construction of a multi-project. Construction of multiple items in a single procedure performed in Gradle. We must participate in the construction of the project declared in the setting.gradle
Project Location
Construction of multi-project can be seen as a single tree. Each project is a node in the tree. A project has a path indicates the position in the tree.
Often the path of the project and the location in the file system is consistent, of course, this path is configurable.
Setting.gradle project tree is generated, default position setting.gradle the case of the position is the root of the project. But you can change the setting.gradle file.
Construction of the project tree
In setting.gradle settings file that you can use to configure a series of methods to build the project tree. Hierarchical and flat physical layout support.
Hierarchical layout
Groovy
include 'project1', 'project2:child', 'project3:child1'
Kotlin
include("project1", "project2:child", "project3:child1")
The method of use include items as its argument, assume that the project file system path relative physical path are equal.
For example "project2: child" corresponding default is relative to the root directory "project2 / child".
This also means that contains the path "services: hotels: api" will create three projects:
- “services”
- “services:hotels”
- “services:hotels:api”
A more detailed description can be DSL document
Layout
Groovy
includeFlat 'project3', 'project4'
Kotlin
includeFlat("project3", "project4")
includeFlat also a directory name as a parameter. To these directories and root project directory at the same level.
The location of these directories is a subproject of the root item in the project tree.
Change the elements of the project tree
Multi-project tree created in the configuration file is described by the so-called project specifier. These bullets can be changed at any time.
The following descriptor can be accessed in this manner
Find the elements of the project tree
Groovy
println rootProject.name
println project(':projectA').name
Kotlin
println(rootProject.name)
println(project(":projectA").name)
This descriptor name you can use a project to build the project directories and files
Change the project tree element
Groovy
rootProject.name = 'main'
project(':projectA').projectDir = new File(settingsDir, '../my-project-a')
project(':projectA').buildFileName = 'projectA.gradle'
Kotlin
rootProject.name = "main"
project(":projectA").projectDir = File(settingsDir, "../my-project-a")
project(":projectA").buildFileName = "projectA.gradle"
More detailed information can be viewed ProjectDescriptor API documentation class.
Receiving life-cycle events
Build scripts can receive the life cycle of building notification schedule.
To receive them are generally two forms
- Implementation details listener interfaces
- A closure performed when sending notification
Project Evaluation Events
Can immediately receive event notifications using Project.afterEvaluate method, passing in a closure, Gradle will evaluate the project and the state passed into the closed bag in the project evaluation.
Kotlin
afterEvaluate {
println("${project.getName()} 评估结果:${state.getExecuted()}")
}
Groovy
afterEvaluate{ project,state->
println "$project 评估成功否:${state.failure==null}"
}
If there is a building in a multi-project, you can use allprojects in a closed bag, so that each project will assess the events have received the
Groovy
allprojects{
afterEvaluate{ project,state->
println "$project 评估成功否:${state.failure==null}"
}
}
Event before the notice of assessment is still using Project.beforeEvaluate pass a closure, Gradle project will be evaluated passed into the closed bag
Groovy
allprojects{
afterEvaluate{ project,state->
println "$project 评估成功否:${state.failure==null}"
}
beforeEvaluate { project ->
println "开始评估 $project"
}
}
Here are the api documentation used.
task
The task is added to the project
Groovy
tasks.whenTaskAdded { task ->
println "$task 被添加到项目了。"
}
Kotlin
tasks.whenTaskAdded {
extra["srcDir"] = "src/main/java"
}
val a by tasks.registering
println("source dir is ${a.get().extra["srcDir"]}")
Directed acyclic graph is completed filling
The method used is TaskExecutionGraph.whenReady
Groovy
gradle.taskGraph.whenReady{ graph->
println "任务图准备好了:\n"
graph.allTasks.each {
print "$it , "
}
}
Task Execution
Groovy
task ok
task broken(dependsOn: ok) {
doLast {
throw new RuntimeException('broken')
}
}
gradle.taskGraph.beforeTask { Task task ->
println "executing $task ..."
}
gradle.taskGraph.afterTask { Task task, TaskState state ->
if (state.failure) {
println "FAILED"
}
else {
println "done"
}
}
Here to stay a query of the address Gradle API
Reference documents