[Switch] learning one of gradle from scratch --- first learning gradle

Source: Internet
Author: User

Original article: http://www.blogjava.net/wldandan/archive/2012/06/27/381605.html

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:

public abstract class AbstractTask implements TaskInternal, DynamicObjectAware {    private List<Action<? super Task>> actions = new ArrayList<Action<?   super Task>>();      public Task doFirst(Action<? super Task> action) {          if (action == null) {              throw new InvalidUserDataException("Action must not be null!");          }          actions.add(0, wrap(action));          return this;     }     public Task doLast(Action<? super Task> action) {         if (action == null) {             throw new InvalidUserDataException("Action must not be null!");         }         actions.add(wrap(action));         return this;     }

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!

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.