Build automation with Gradle

0
452

When you start programming, the main key to the success of your job is identified in the code. Its quality is certainly an essential element. A more professional approach, however, suggests the adoption of additional tools that increase the productivity of a programmer: development environments, a good debugger, version control systems but also build automation tools. In the latter category belongs Gradle, multiplatform and free software, among the most advanced currently available in the build automation.

In this article we will introduce some basic concepts, common to all tools of this type, and then deepen the use of Gradle.

What is build automation

The software development phases are surrounded by a series of ancillary activities, sometimes preliminary, sometimes implemented retrospectively, often organized during development. Among these activities we can highlight the main ones:

  • compilation: this is the process of transforming the sources (written by the programmer) into binary code (the format recognized by the machine);
  • track packaging: the result of the compilation must take on a compact form and a clear connotation according to its purpose: executable, library to export, etc .;
  • automated test execution: a true software cannot simply be “tested”. It is not enough to enter some random data with your own hands and feel satisfied with the results obtained. To avoid hidden bugs, it is necessary to carry out an experimental phase that demonstrates the reliability of the software in every facet, trying to verify its functionality in every possible situation. To follow a test strategy of this kind, a human being of goodwill is no longer enough, but a machine is necessary, a software specially designed to perform complete tests;
  • solution deployment: once the software is ready and tested, it must be installed and configured on the systems for which it has been implemented;
  • documentation of the project: the software products are often quite complex and live, through the published versions, a real evolution. Given the complexity of IT products, the preparation of appropriate documents explaining their use and functionality is essential. Obviously, the documentation will take the form most suited to the type of project and to the user target.

The build automation products are tools designed to automate all the steps just listed. Their importance is remarkable, to the point that they have populated the history of computing. If programmers have relied on Make for so many years , experts in the Java world have often compared themselves with Ant and Maven , both based on XML configuration files. Despite the differences that distinguish them, the build automation tools sometimes have common traits, such as:

  • the build file: the operations that must be performed to transform the source project into the final solution are collected in the main file, which sometimes refers to or includes other secondary files;
  • the task: is the concept that defines the single operation to be performed within the phases of the build process. Often the tasks are divided into micro-actions and are contextualized by assigning their precedence and dependencies to other tasks. In this way the flow of the entire build process is defined;
  • the dependencies of software from other projects: a professional level, you do not start program “from scratch” but it is based on frameworks, libraries or other projects, which form the foundations of our work. Making sure that during the various build phases the project has everything necessary is a painful task for the programmer. A great merit of the tools of this category nowadays is to automate the fulfillment of the project dependencies automatically, through the network, making available the updated versions of the dependencies, or those better compatible with our needs.

Ultimately, an IT project comes to life thanks to a series of phases, sometimes complex, whose automation raises the programmer from considerable responsibility and reduces the possibility of errors of distraction or fatigue.

Gradle: let’s start using it

Build automation tools like Ant and Maven, rich in functionality and expandable, have often demonstrated a certain configuration rigidity due mostly to the use of the XML format. Gradle is proposed as one of the most advanced software management tools of the moment. It benefits from the use of configurations carried out in a declarative language: GGroovy. It is a scripting language that was born within the Java technology. It belongs to object-oriented programming and its code is compiled on-the-fly and takes the form of bytecode for the Java Virtual Machine.

Gradle is a multiplatform project and can be downloaded from the reference site www.gradle.org . Following the download link you can download the sources, the binaries or a package that includes them both with the addition of documentation. We choose to download the version with the binaries and, once unzipped the archive in .zip format , we will get the content partially visible in the following figure.

Among the various folders, there are two of greater interest for our purposes:

  • bin: contains the executables. They will be used to activate the command line program;
  • lib: collects many jar archives that will allow Gradle to do its job. A quick look inside this directory points out some names that demonstrate on what solid bases rests Gradle and how deeply rooted in the Java world: ant, log4j, groovy, different commons of Apache and much more.

Once the .zip archive has been unpacked, Gradle is ready for use. To work, it requires nothing more than a properly configured JDK or JVM in the system. To test its functionality, we immediately use the command gradle, directly from the command line:

gradle -v

On Ubuntu, the output shown is as follows:

<pre class=”brush: php; html-script: true”>
————————————————————
Gradle 2.1
————————————————————

Build time: 2014-09-08 10:40:39 UTC
Build number: none
Revision: e6cf70745ac11fa943e19294d19a2c527a669a53

Groovy: 2.3.6
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.7.0_21 (Oracle Corporation 23.21-b01)
OS: Linux 3.5.0-23-generic amd64
</pre>

As you can see, the command just typed shows the version of Gradle (2.1 in this case) and all the components it has available: JVM, groovy, ant and operating system.

The syntax

Since an important advantage of Gradle consists in the flexibility of the language used, let’s see it immediately at work. We create a build file, which we can call build.gradle, open it with a text editor and write the following code inside it:

import java.text.SimpleDateFormat
SimpleDateFormat simple=new SimpleDateFormat("HH:mm:ss")
task proviamoGradle {
    for(int i=1;i&lt;20;i++)
        if (i%2==0) println i+" è pari..."
            println 'Operazioni terminate alle'+simple.format(new Date())
}

From the command line, type:

gradle proviamoGradle

obtaining the following output:

<pre class=”brush: php; html-script: true”>
2 è pari…
4 è pari…
6 è pari…
8 è pari…
10 è pari…
12 è pari…
14 è pari…
16 è pari…
18 è pari…
Operazioni terminate alle 12:33:35
</pre>

In practice, we have created a program, whose heart is placed in the task called proviamoGradle , in which the even numbers between 1 and 20 are printed, followed by the hourly information of the end of the operations. The most interesting thing about this, though, is the fact that the code we used is nothing but Java .

The classes Dateand SimpleDateFormatwe used are exactly those belonging to the JDK, forand ifare the control constructs we know well and printlnis the instruction to request the printing of a single line console.
The example shows how the use of Groovy within Gradle not only gives flexibility to instructions, but all the richness of the functionality of the Java world.

LEAVE A REPLY

Please enter your comment!
Please enter your name here