Learn Gradle-ch 2 Basic Build Script Introduction

Source: Internet
Author: User

1. Projects and Tasks

The Gradle build script consists of two basic concepts, namely the project (projects) and the task (tasks).

Projects refer to our build artifacts (such as jar packages) or implementation artifacts (such as Web application, etc.). The gradle build script contains one or more projects .

Tasks are the smallest unit of work that can be divided, and perform build work (such as compiling some class files, creating jar files, generating Javadoc, and publishing schema documents to warehouses, etc.). One project contains one or more Tasks .

2. Hello world!!

Let's learn a simple Hello World example to get a simple look at the Gradle build script.

New file: Build.gradle

Add Content:

Task Hello {dolast {println ' Hello world! ' }}

Use the command line to enter the directory where the Build.gradle is located, execute:gradle Hello, output:

: Hellohello world! BUILD Successful

In this example, the build.gradle file is a build script (strictly speaking, this is a build configuration script), which defines a project and the tasks that the project contains.

Gradle is a building tool for domain-driven design, where the project interface corresponds to the project concept above, the task interface corresponds to the task concept above, and in fact there is an important domain object, the action, Corresponds to a specific operation within a task. A project consists of multiple tasks, and a task is made up of multiple actions.

When the gradle hello is executed, gradle will call this hello task to perform the given action (action). This operation is actually a closure written in groovy code, where the task is a method in the project class, creating a Task object by invoking the task method here, and passing in println ' Hello world! ' in the object's Dolast method This closure. This closure is an action.

A task is an interface defined in Gradle that represents a task in the above concept. It defines a series of abstract methods such as Dolast, Dofirst, and so on, which can be seen in the Gradle API Org.gradle.api.Task documents.

3. Use shortcut keys to define tasks

Dolast has another simple notation:

Task Hello << {println ' Hello world! '}

Executes the gradle Hello command, with the same output as before. That is, we have a code like Dolast, directly simplified to << this symbol. This is actually the gradle use of Groovy's operator overloading feature, the left shift operator is implemented to add the action to the end of the task, the equivalent of calling the Dolast method. Look at the Gradle API document in the Dolast () and Leftshift () the introduction of the two methods, it is known that their role is the same, so here,<< left operator is dolast shorthand.

4, the script is the code

In the Gradle build script, you can use groovy code to achieve more powerful functionality.

Example 1:

Task Upper << {String somestring = ' my_name ' println "Original:" + somestring println "upper case:" + S Omestring.touppercase ()}

Execute Gradle Upper, output:

: Upperoriginal:my_nameupper case:my_namebuild Successful

Example 2:

Task Count << {4.times {print "$it"}}

Execute gradle count, output:

: Count0 1 2 3BUILD successful
5. Task Dependency

1. Define the task dependencies in the script:

Task Hello << {println ' Hello world! '} Task Intro (Dependson:hello) << {println "I ' m Gradle"}

Execute gradle intro, Output:

: Hellohello World!:introi ' m gradlebuild successful

2. Tasks can depend on tasks that have not yet occurred:

Task Taskx (dependsOn: ' Tasky ') << {println ' taskx '}task Tasky << {println ' Tasky '}

In this case, the task Taskx relies on Tasky, but Tasky is not defined until after taskx. Execute Gradle taskx, output:

: Taskytasky:taskxtaskxbuild Successful
6. Dynamic Tasks

We can dynamically create tasks using Groovy's syntax, for example:

4.times {counter, task "Task$counter" << {println "I ' m task number $counter"}}

Execute gradle-q task1, output:

: task1i ' m task number 1BUILD successful
7. Manipulating Tasks

The task that has been created can be accessed through the API (example 1, the API is DependsOn).

Example 1: Adding Dependencies to a task

4.times {counter, task "Task$counter" << {println "I ' m task number $counter"}}task0.dependson t Ask2, TASK3

Perform gradle task0, output

: Task2i ' m task number 2:task3i ' m task number 3:task0i ' m task number 0BUILD successful

Example 2: Adding Behavior to a task

Task Hello << {println ' Hello Earth '}hello.dofirst {println ' Hello Venus '}hello.dolast {println ' Hello M Ars '}hello << {println ' Hello Jupiter '}

Execute gradle Hello, output:

: Hellohello Venushello Earthhello Marshello jupiterbuild Successful

The task executes the Dofirst first, and then sequentially executes the Dolast ("<<" can be interpreted as an alias of Dolast, so the same API method will be executed in the configuration file order)

8. Custom Attributes
Task MyTask {ext.myproperty = "myvalue"}task printtaskproperties << {println Mytask.myproperty}

Execute Gradle printtaskproperties, output:

:p Rinttaskpropertiesmyvaluebuild Successful
9. Default Task

Gradle allows you to configure one or more tasks to execute as default tasks during the build process, such as:

Defaulttasks ' clean ', ' run ' task clean << {println ' Default cleaning! '} Task run << {println ' Default running! '} Task other << {println "I ' m not a default task!"}

Execute Gradle, Output:

: Cleandefault Cleaning!:rundefault running! BUILD Successful
10, DAG (Directed acyclic graph, directed non-circular graph) configuration

gradle the life cycle of the build consists of the initialization phase, the configuration phase, and the execution phase. Gradle uses Dags to record the order in which tasks are executed. After the configuration phase is complete, gradle is clear that all needs to be performed are tasks that will be stored to taskgraph . Gradle provides a hooks to use this (taskgraph ) information. In this example we will judge

Task distribution << {println "We build the zip with version= $version"}task release (dependsOn: ' Distribution ') &         lt;< {println ' We release now '}gradle.taskgraph.whenready {taskgraph-and if (Taskgraph.hastask (release)) { Version = ' 1.0 '} else {version = ' 1.0-snapshot '}}

Execute gradle distribution, output:

:d Istributionwe build the zip with Version=1.0-snapshotbuild successful

Execute Gradle release, output:

:d Istributionwe build the zip with Version=1.0:releasewe release Nowbuild successful


Learn Gradle-ch 2 Basic Build Script Introduction

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.