Site icon Pro Liferay

Explore Gradle as Build Tool

gradle-as-build-tool


Gradle 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

Note : 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

---sample
    |   
    \---src
            Sample.java

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.

\---sample
    |   build.gradle
    |   
    \---src
            Sample.java

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

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

Where
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
:wrapper
:init

BUILD SUCCESSFUL

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
|   
+---.gradle
|   \---2.10
|       \---taskArtifacts
|               cache.properties
|               cache.properties.lock
|               fileHashes.bin
|               fileSnapshots.bin
|               outputFileStates.bin
|               taskArtifacts.bin
|               
+---gradle
|   \---wrapper
|           gradle-wrapper.jar
|           gradle-wrapper.properties
|           
\---src
    +---main
    |   \---java
    |           Library.java
    |           
    \---test
        \---java
                LibraryTest.java

Summery:
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.

Exit mobile version