In the previous article, we mentioned some basic concepts of gradle, such as project, task, and action, and created our first task. This time, let's take a look at more details about projects and tasks in gradle.
1. Project and task
For the build. gradle configuration file, when gradle <task> is run, gradle creates a project object for us to map the content in build. gradle. Gradle creates a script Class Object for code that does not belong to any task category to execute the Code. For the definition of a task, gradle creates a task object, and use it as the property of the project (actually completed by gettaskname ). OK. Let's look at a simple example:
Create the basic/build. gradle file and add the following code:
Println "The project name is $ name"
Task Hello <{
Println "the current task name is $ name"
Println "Hello World"
}
When running this example, gradle will first create a Porject object and map it with the build. gradle content. In this example, the project consists of two parts:
1) executable script Definition
As mentioned earlier, the executable script definition will be directly created as the corresponding script class object.
In this example, the script corresponds to the println of the first line, and the execution result is "The project name is basic ".
By default, the project name is the name of the directory where the current build. gradle is located. In this example, build. gradle is placed under the Basic directory. Therefore, the project name is basic.
2) task definition
In this example, gradle creates a task instance and associates it with the defined task content. In addition, as mentioned earlier, when gradle is running, we can access it by accessing the project attribute.
For example, in this example, we can use project. Hello to access this task. Because this task Hello has become an attribute of the project, when we use gradle properties (properties is a built-in task of gradle, it can list all attributes of the current project level, you can use gradle tasks to view more built-in tasks) to obtain the attribute list at the project level. You will also get 'Hello '.
In addition, in this example, the $ name used in the task is not the name of the project, but the name of the current task, because it is used in the scope of the task.
Execute gradle hello and the output result will be:
Current Project name is test
The current task name is hello
Hello World
2. Define attributes
In gradle, we can define the following three attributes and use them:
1) System Properties
System properties actually refers to the JVM system properties. We know that when running a Java program, you can use-D to set the java system variables. In gradle, you can also do the same. For example
Gradle XXX-dmysystemprop = xxxx
At the same time, in build. gradle, you should use this variable as follows:
Task printsysprops <{
Println system. properties ['system']
}
2) Project Properties
Project properties are the properties defined by gradle for the project. Its biggest advantage is that when running gradle, we can use-P to set its value. For example,
Gradle XXX-pmyprojectprop = XXXXX
In build. gradle, you can use this variable as follows:
Task printprops <{
If (project. hasproperty ('commandlineprojectprop ')){
Println commandlineprojectprop
}
} At the same time, when we execute gradle properties to view the attribute list, the name and value of this variable will be displayed in the result.
3) ext (RA) Properties
In addition, we can also define ext attributes for projects or tasks, also known as dynamic attributes. We must use the keyword ext (corresponding to the instance of extrapropertiesextension) to define dynamic attributes. From this point, we can see that gradle has designed many different classes for us to do different things. We only need to follow the Convention and use them. If you forget to write the ext keyword, the following prompt will be displayed during gradle runtime:
"Dynamic properties are deprecated ....". This is because the previous version of gradle does not need to add the ext keyword when defining dynamic attributes.
For projects and tasks, dynamic attributes are defined in the same way, but with different scopes.
After the definition is complete, we can use project. Prop or task. Prop to access these dynamic attributes. Let's take an example:
Ext. projectproperties = "ext projectproperties-value"
Task printextprops <{
Ext. taskproperties = "Ext. task. properties-value"
If (project. hasproperty ('projectproperties ')){
Println "Ext. projectproperties values is" + projectproperties
}
If (printextprops. hasproperty ('taskproperties ')){
Println "task has defined Ext. taskproperties value is" + taskproperties
}
}
NOTE: For the dynamic attribute defined by Ext, you cannot modify its value externally. You can only set or modify its value in build. gradle.
In addition, if the ext dynamic attribute is defined for the project, it will be displayed in the gradle properties result.
3. Task dependency
When building a complex project, dependencies between different tasks are inevitable. For example, if you want to run a 'deploy' task, you must first run tasks such as compilation, packaging, and detection servers. Only after these tasks are executed by the dependent tasks, to be deployed. Ant and Maven provide declarative definitions for dependencies between such behaviors, which is very simple. Similarly, it is easy to use gradle to define dependencies between tasks.
For example, the following two tasks are defined and the keyword "dependendson" is added to "Intro", as shown below:
Task Hello <{
Println 'Hello world! '
}
Task intro (dependson: Hello) <{
Println "I'm gradle"
} Execute "gradle Intro" and the result will be:
Hello World
I'm gradle
It can be seen that when gradle intro is executed, the task hello on which intro depends will be executed first. In addition, dependenson also supports defining dependencies for multiple tasks, which can be included in. For example
Task A (dependenson: ['B', 'C', 'D']) <{XXX}
In addition to dependenson and string to define dependencies, we can also use taskx. dependenson tasky:
Task taskx <{
Println 'taskx'
}
Task <{
Println 'tasky'
}
Taskx. dependson tasky
Alternatively, you can use the closure:
Task taskx <{
Println 'taskx'
}
Taskx. dependson {
Tasks. findall {task-> task. Name. startswith ('lib ')}
}
Task lib1 <{
Println 'lib1'
}
In addition to the tasks mentioned earlier, gradle also provides many available APIs. For more details, refer to the gradle documentation. Here I will list a common method onlyif. In gradle, we can use "onlyif ()" to determine whether the current task needs to be executed. For example, create build. gradle and add the following code:
Task Hello <{
Println 'Hello world'
}
Hello. onlyif {! Project. hasproperty ('skello ello ')}
When "gradle hello" is executed directly, there is no result. When "gradle hello-pskiphello = XXXX" is executed, "Hello world" is output ".
4. Summary
OK. This is the summary of gradle today. In general, it is still very fancy to use DSL code instead of XML to define the building process.
Learning gradle 2 from scratch --- How to Use tasks