Learning Gradle from the beginning of zero---the first knowledge gradle_gradle

Source: Internet
Author: User

Prerequisite: Install Gradle. The installation process is simple:
(1) Download Gradle
(2) Add Gradle_home/bin/gradle to $path.

1. Basic Concepts (Project and Task)

There are two basic concepts in Gradle: Project and task. Each gradle build is composed of a project that represents the components that need to be built or the entire project that is built. Each project consists of one or more tasks. A task represents the smallest unit that can be executed during the Gradle build process. For example, when building a component, you might need to compile, package, and then generate documents or publications, and each of these steps can be defined as a task.


2. Building the first task
Similar to the ant Run-time read Build.xml, the Gradle runtime reads the Build.gradle file by default, but you can also use the parameter "-B" to specify the other Xxx.gradle

Next, let's create a new Build.gradle file and enter the following: task Hello {
dolast{
println "Hello World"
}
}

This build script is very simple, is output Hello world. In order to run this build, we should execute "gradle hello" in the current directory, that is, Gradle taskname.
Dolast means defining an action (the action class in the map Gradle), placed at the end of the current task, similar, and Dofirst, which means that the defined behavior is placed at the very front of the current task, such as
Task Hello {
dolast{
println "Hello World"
}
dofirst{
println "I am XXX"
}
Execute gradle Hello and will output
"I am XXX"
"Hello World"

In addition, you can define tasks in a more concise manner as follows:

Task Hello << {
println "Hello World"
}

Here, perhaps, you might find it strange why you can use "<<" to define the task's execution, or let's see how the Gradle code is implemented:
1 public abstract class Abstracttask 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 to be null!");
7}
8 Actions.Add (0, Wrap (action));
9 return this;
10}
11
The public Task dolast (action<? Super task> Action) {
if (action = null) {
throw new Invaliduserdataexception ("Action must not to be null!");
15}
Actions.Add (Wrap (action));
return to this;
18}
As you can see from the above code, the task class has an action set of actions that, when used Dofirst or dolast, actually instantiates the defined execution part as an action object and then adds it to the actions collection.
With that in mind, let's see why we can use the << definition task--------Groovy as a powerful DSL-enabled dynamic language that has already overloaded << operators, allowing us to easily add elements to the collection with <<.
Speaking of this, believing that the truth has been revealed: the original use of groovy features, to add the action in the collection. Yes, this is Gradle's syntax, and using Groovy's DSL features helps us to define our build scripts more easily.
However, it may be felt that this example is really not representative, just a simple Hello world, does not explain what the problem. Well, don't worry, we'll continue to study the rest of the Gradle next time, but remember: as a building tool, Gradle is really powerful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.