Prerequisites: Install gradle. The installation process is very simple:
(1) download gradle
(2) Add gradle_home/bin/gradle to $ path.
1. Basic concepts (project and task)
Gradle has two basic concepts: Project and task. The building of each gradle is composed of a project, which represents the components to be built or the entire project to be built. Each project consists of one or more tasks. Tasks represent the smallest units that can be executed during the gradle build process. For example, when building a component, you may need to compile, package, and then generate documents or publish. Each step can be defined as a task.
2. Build the first task
Similar to reading build. xml during ant running, gradle will read the build. gradle file by default. You can also use the parameter "-B" to specify other XXX. gradle files.
Next, let's create a build. gradle file and enter the following content:
Task Hello {
Dolast {
Println "Hello World"
}
}
This build script is simple, that is, output Hello world. To run this build, we should execute "gradle hello" in the current directory, that is, gradle taskname.
Dolast defines an action (mapped to the action class in gradle) at the end of the current task. Similarly, dofirst indicates that the defined behavior is placed at the beginning of the current task, for example
Task Hello {
Dolast {
Println "Hello World"
}
Dofirst {
Println "I am XXX"
}
} Execute gradle hello to output
"I am XXX"
"Hello World"
In addition, you can use the following more concise method to define a task:
Task Hello <{
Println "Hello World"
}
Here, you may find it strange: Why can we use "<" to define the execution content of a task? Let's see how gradle's code is implemented:
1 public abstract class implements acttask implements taskinternal, dynamicobjectaware {
2 Private list <action <? Super task> actions = new arraylist <action <? Super task> ();
3
4 public task dofirst (Action <? Super task> action ){
5 If (Action = NULL ){
6 throw new invaliduserdataexception ("action must not be null! ");
7}
8 actions. Add (0, wrap (Action ));
9 return this;
10}
11
12 public task dolast (Action <? Super task> action ){
13 if (Action = NULL ){
14 throw new invaliduserdataexception ("action must not be null! ");
15}
16 actions. Add (WRAP (Action ));
17 return this;
18}
From the code above, we can see that the task class has a set of actions. When dofirst or dolast is used, it actually converts the execution part of the definition into the action object, then add it to the actions set.
After understanding this, let's take a look at why we can use <define task -------- groovy as a powerful dynamic language supporting DSL, which has already been overloaded <operator, this makes it easy to use <add elements to the set.
Speaking of this, I believe the truth has been revealed: It turns out that adding action to the set is just using groovy features. Yes, this is the gradle syntax. Using the DSL feature of groovy, we can easily define our build scripts.
However, you may think that this example is really not representative. It is just a simple hello World, which cannot be explained. Well, don't worry. Next time we will continue to study other parts of gradle, but remember: As a build tool, gradle is really powerful!
Learning gradle from scratch --- first learning gradle