Gradle – Basic Introduction

Gradle – an object-oriented build tool.
Alternatives to GRADLE: ANT, MAVEN.

Gradle is an object-oriented build automation tool (compile, package and deploy your application). It provides sensible defaults by using convention-over-configuration. Gradle is an open source project and it is licensed under the Apache License.

With Gradle you can automate the compiling, packaging, testing and deployment of your project. Although you can rely on defaults, Gradle offers the flexibility to adapt a build to certain custom needs.

Gradle uses Groovy DSL (domain specific language) to express project’s build logic.

Benefits of GRADLE

  • INCREMENTAL BUILDS – which implies, the tasks in a build are only executed if necessary. Meaning, a task to compile source code will fist check whether the source code have change since it’s the last execution. If the source code was change, the task is executed, otherwise it is skipped and marked as being up to date;
  • SUPPORT FOR ANT TASKs – within Gradle, you can import an ANT build and reuses all the ANT tasks. You can also write Gradle tasks dependent on ANT tasks;
  • MAVEN and IVY REPOSITORIES – with Gradle you can rely on the repository infrastructure that you have, as Maven and Yvi is are supported to publish or fetch dependencies;
  • SUPPORT FOR MULTI-PROJECT BUILDS – with Gradle you have the flexibility to define a graph of dependencies among projects and it can resolve those dependecies;

Gradle Wrapper

Gradle Wrapper permits to execute gradle builds even if gradle is not installed on a computer. The Gradle Wrapper is a script build on top of the operating system (batch script in case of Microsoft Windows, and shell script in case of UNIX-based operating systems) that will download Gradle and run the build using that downloaded version of Gradle.

In the gradle script file you can define the Gradle version and by running it via the wrapper, that version of Gradle will be downloaded and used to run the script.

To use Gradle, you need to install JDK 1.6 or greater, and obviously Gradle. It’s not within the scope of this blog post to cover the installation of JDK or Gradle. Therefor I will assume you already have JDK and Gradle on your system.

Anatomy of Gradle

In Gradle is all about gradle build scripts, which use the concept of projects and tasks to define them. A gradle build can have one or more projects (a project, is a set of components that we want to build for our application). A project has one or more gradle tasks (unit of work that needs to be executed by the build).

Out of the box, Gradle creates objects of both project and task types.

Examples of tasks are:

  • compiling source code;
  • packaging class files into a jar file;
  • running tests;
  • deploying the application.

A task is made up of actions which are executed when task is executed. Gradle supports two common ways to add actions to your task:

  • doFirst() method – will add action to the beginning of the list of actions.
  • doLast() method – will add action to the end of the list of actions.

Here is an example on how to use these tasks:

Within Gradle you can use the dependsOn() method to define dependencies between tasks:

Running a gradle build

To run a gradle build, you will use the gradle command. Running this command, Gradle will look, in the current directory, for a file named build.gradle (this file is the build script for underlying project).

Gradle built-in tasks

Gradle came with various built-in (predefined) tasks that you can run. use the gradle -q tasks command to ask gradle to show the available tasks for your project.

Define a gradle task

Gradle tasks are define inside build.gradle file which contains the tasks that make up the project. In this example I will show how to define a simple task inside build.gradle and print out a message to the console:

Gradle build phases

A Gradle build passes through three stages: initialization, configuration and execution.

In the initialization phase, Gradle figures out whether the current project has child projects, or it is the only one one in the build. Forward, Gradle creates the project instance of the root project, and for each of the child project (if any).

In the configuration phase, the build scripts of projects are evaluated. When a script is evaluated, all the statements in the script are executed sequentially. In this phase, all tasks are configured, aka: Gradle prepares a Directed Acyclic Graph (DAG) representation of the tasks to determine the task dependency and execution order.

Keep in mind that a task action, is actually a closure. Which translates into “it is only attached to a task during the statement execution”. The closure itself is not executed yet. The statements inside the action closure executes only if the task is executed, which happens only in the execution phase.

The execution phase is where tasks actions are performed.

Gradle plugins – real life work with Gradle

In Gradle, plugins add extra functionalities to our project. A plugin can contain tasks, configurations, properties, methods, concepts, etc.

You can write your own plugin or use one which is shipped with the Gradle distribution

Examples of Gradle built-in plugins:

  • java plugin – this plugin ads tasks for compiling, testing, and packaging java source code.
Spread the love

Leave a Reply