The article aims to briefly describe key concepts of Gradle along with providing code samples for hello world. The intended audience are those inquisitive souls who want to quickly get up and running with Gradle tool. For detailed information, you may want to check out the book, Building & Testing with Gradle.

• Coding Build Files with Groovy DSL
• Closures
• HelloWorld Code Samples

As defined on http://www.gradle.org/, “Gradle is build automation evolved. Gradle can automate the building, testing, publishing, deployment and more of software packages or other types of projects such as generated static websites, generated documentation or indeed anything else.

Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build.”

Gradle executes a build through three lifecycle phases: initialization, configuration, and execution. Initialization is the phase in which Gradle decides which projects are to participate in the build. Configuration is the phase in which those task objects are assembled into an internal object model.Execution is the phase in which build tasks are executed in the order required by their dependency relationships.

###### Coding Build Files with Groovy DSL
• Gradle build files syntax in Groovy language. It was chosen this way and NOT XML owing to drawbacks that XML has in relation with readability and hierarchical structure that does not fit very well in terms of representing program flow and data access. In addition, one could program one or more tasks, thanks to Groovy language.
• Given Groovy, one would get tempted to write complex conditional-based build tasks. However, after a while, this may become cumbersome to maintain such code. Thus, Gradle provides domain-specific language (DSL) tailored to the task of building code.

• Methods
• doFirst { <action code> }
• doLast { <action code> }
• onlyIf { <action code> }
• Properties
• description
• didWork
• enabled
• path
• logger
• logging
• temporaryDir

Following is the sample code of a helloworld task. Detailed code is provided later in the article.

task hello << {
println 'hello world'
}
###### Closures

Code between curly braces shown in above examples can be termed as Closures. This code block could be seen as objects which could be passed to methods like an argument or assigned to variable. In above example, code between curly braces such as println ‘hello world’ could be termed as closure.

Following is the code samples that represents some of the following key aspects:

• Methods such as doFirst, doLast, onlyIf

Code Samples

task hello

pre_hello << {
println 'executing pre-hello...'
}
// Configuration for hello task; Notice that there is no << sign
hello {
dependsOn pre_hello // Note that dependsOn needs to be called from configuration block only.
println 'configuration block...'
}
hello << {
println 'hello ' + System.properties['name']
}
// The doFirst method is called before the execution of task hello
hello.doFirst {
println '--- HelloWorld Program ---'
println ''
}

// The onlyIf method is used as conditional making sure that hello task gets executed only if condition satisfies
hello.onlyIf {
System.properties['name'] != ''
}

// The doLast method is called after the execution of task hello
hello.doLast {
println ''
println '--- Hope you liked it! ---'
}

###### Execution Steps

Install & configure Gradle as per the instructions manual. Once installed, create a gradle file named as build.gradle and paste the code shown above. Execute the command “gradle <taskname>”. In above case, it would be gradle -Dname=vitalflux hello