• Explore Gradle as Build Tool

    Posted on June 12, 2016 by Hamidul Islam in Gradle.


    aimGradle is open source tool which is widely used to automate our build system. Gradle combines the good parts of both Ant and Maven tools. Gradle provides additional features and uses Groovy as a Domain Specific Language (DSL). It has power and flexibility of Ant tool with Maven features such as build life cycle and ease of use. Gradle is already used by big open source projects such as Spring, Hibernate and Grails. Enterprise companies such as LinkedIn also use Gradle. In this article we will explore the basics of Gradle Tool.

    With Gradle we can ... 

    ♦ Automate the compiling

    ♦ Testing 

    ♦ Packaging

    ♦ Deployment

    1. Gradle Installation

    i) Download Gradle from https://services.gradle.org/distributions/gradle-2.14-rc-6-bin.zip or latest.

    ii) Unzip the Gradle zip to any one of your preferred directory.

    iii) Set the environment variable something like below and add %GRADLE_HOME%\bin; in the Path variable

    gradle-homeNote : We have shown the environment variable as in Windows Machine. For Linux and MAC  machine try yourself.

    2. Testing the Gradle Installation

    Open the command prompt and execute gradle -version. You should see below message


    3.Gradle Build File 

    i) In Gradle, the build file name is build.gradle. A gradle build file can contains many tasks. A sample build.gradle

    task hello << {
        println 'Hello world!'

    In the above hello is the gradle task. The hello task can be executed by below command

    gradle hello

    ii) Build a Java project by Gradle
    Lets consider that we have very simple java project with below directory structure where sample is the root folder of the project


    Now we want to build the Sample project by Gradle which is very simple. Just open your preferred editor and create a build file with name gradle.build and place it in sample directory. The content of gradle.build should be something like this

    apply plugin: 'java'
    sourceSets {
        main {
            java {
                srcDir 'src'

    Note 1: apply plugin: 'java' used for java project
    Note 2: sourceSets is to tell where is source code available.

        |   build.gradle

    Build the sample project by the below command in the command prompt

    gradle build

    iii) Java plugin default project layout

    In the above example we have used java plugin in the build.gradle file. By default it will look source code as below

    |   \---src
    |       +---main
    |       |   +---java
    |       |   \---resources
    |       \---test
    |           +---java
    |           \---resources

    src/main/java : Java source
    src/main/resources: Resources like properties file
    src/test/java: Test Java source
    src/test/resources: Test resources

    Note: We will discuss more about the java plugin in our future article to keep this article short.

    4. Generate directory structure of java project

    We can also generate directory structure of standard java project. This can be done by executing a single command as

    H:\gradle-examples>gradle init --type java-library
    Total time: 6.75 secs

    After successful execution of the above command it will generate below structure

    |   build.gradle
    |   gradlew
    |   gradlew.bat
    |   settings.gradle
    |   tree.txt
    |   \---2.10
    |       \---taskArtifacts
    |               cache.properties
    |               cache.properties.lock
    |               fileHashes.bin
    |               fileSnapshots.bin
    |               outputFileStates.bin
    |               taskArtifacts.bin
    |   \---wrapper
    |           gradle-wrapper.jar
    |           gradle-wrapper.properties
        |   \---java
        |           Library.java

    There are many interesting topics for Gradle. In this article we just briefed about the Gradle tool to get basic idea. We will write more on it to cover various aspects of Gradle.

    Post Tagged with
Comments are closed.
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.