Introduction To Gradle For Developing Multiple Projects

Introduction

 
In this article, we discuss Gradle and also have a look at its features, the installation guide, its advantages and create a simple program using Gradle.
 

What is Gradle

 
Gradle is built automation evolved. Gradle is a build system that offers you ease, power and freedom. You can choose the balance for yourself. It has powerful multi-project build support. It can automate the building, publishing, testing, deployment and more of software packages or other types of projects such as developing static websites, documentation or indeed anything else.
 
Gradle combines the power and flexibility of Apache Ant with the dependency management and conventions of Apache Maven into a more effective way to build. Powered by a Groovy DSL instead of the more traditional XML form of declaring the project configuration and packed with innovation, Gradle provides a declarative way to describe many kinds of builds through sensible defaults. Gradle is quickly becoming the build system of choice for many open source projects, leading-edge enterprises and legacy automation challenges. The initial plug-in is primarily focused around Java, Groovy and Scala development and deployment, but more languages and project workflows are on the roadmap.
 
To learn more about Gradle, including what it makes, get started with Gradle right now.
 
Download Gradle from the following link:
 
 

Gradle Installation

 
Step 1- First of all Gradle requires a Java JDK to be installed on your system. It requires a JDK 1.5 or higher version. It has its own Groovy library, so they don't require any other Groovy library. Any existing Groovy installation is ignored by Gradle.
 
It uses whichever JDK it finds in your path (for checking Java version, use the command "java -version"). Alternatively, we can set the JAVA_HOME environment variable to point to the install directory of the desired JDK.
 
For example:
 
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_21;
 
Step 2- Unpacking the zip file
 
After unpacking the downloaded zip file, the full distribution contains:
  • Gradle binaries files.
  • User guide file (in HTML and PDF formats).
  • DSL reference guide.
  • API documentation (Groovydoc and Javadoc).
  • Extensive samples.
  • The binary sources. This is for reference only.
Step 3- Set the environment variable for Gradle.
 
To run Gradle, we must add GRADLE_HOME (the folder path of where we copied Gradle) to our PATH environment variable. Usually, this is sufficient to run Gradle.
 
In the path environment variable simply add GRADLE_HOME/bin (for example "E:\gradle\bin;").
 
Step 4: How to run or check the installation work or not?
 
To run Gradle we must use the "gradle" command. For checking that Gradle is properly installed just type "gradle -v". That shows the Gradle version and also the local environment configuration (Groovy and JVM version, etcetera). The displayed Gradle version should match the distribution you have downloaded.
 
The following figure shows the output of the gradle command:
 
Fig-1.jpg
 
When we run the command "gradle -v" it shows the version of Gradle installed in your system, as in the following figure:
 
Fig-2.jpg
 
Step 5- JVM Options
 
This option runs Gradle to set the environment variables. We can use JAVA_OPTN or GRADLE_OPTN. Both variables can be used together. JAVA_OPTN is an environment variable that can be shared by many Java applications.
 

Advantages of Gradle

  • It can be used for various purposes as discussed above; it's a much better Swiss army knife than Ant, but it's specifically focused on multi-project builds. 
     
  • First of all, it is a dependency programming tool that also means it's a programming tool. With the use of Gradle, we can execute any random task in our setup and it will ensure all declared dependencies are properly and timely executed. Our code can be spread across many directories in any kind of layout (tree, flat, scattered, ...).
     
  • On top of these dependencies, programming features Gradle adds project and JAR dependency features by integration with Apache Ivy. As you know Ivy is more powerful and much less opinionated dependency management tool than say, Maven.
     
  • It detects dependencies among projects and JARs. It works with Maven repositories (download and upload) like the iBiblio one or our own repositories but also supports the other kinds of repository infrastructure we might have.
     
  • In multi-project builds, Gradle is both adaptable and adapts to the build's structure and architecture. We don't need to adapt our structure or architecture to our build tool as would be required with Maven.
     
  • Gradle has two distinct phases: evaluation and execution. Basically, during evaluation Gradle will look for and evaluate build scripts in the directories it is supposed to look in. During execution, Gradle will execute tasks that have been loaded during evaluation taking into account task inter-dependencies.
     
  • It tries very hard not to get in our way, an effort Maven almost never makes. A convention is good, yet so is flexibility. Gradle provides many more features than Maven does but the most important in many cases is that Gradle will offer us a painless transition path away from Maven.
Creating a simple program to print "Welcome To Gradle"
 
You run a Gradle script using the gradle command. The gradle command looks for a file called welcome.gradle in the current directory. We call this welcome.gradle file a build script, although strictly speaking it is a build configuration script, as we will see later. The build script defines a project and its tasks.
 
To try this out, create the following welcome script named welcome.gradle.
 
The first welcome script
 
Compilation
  1. task Welcome  
  2. {  
  3.     doLast  
  4.       {  
  5.         println 'Welcome To Gradle!'  
  6.       }  
  7. }  
For compiling our program we have the command gradle -q Welcome:
 
Output
 
gradle -q Welcome
 
Welcome To Gradle!