Gradle
Gradle
Using
Gradle, we can reduce Project Development time and increase Productivity.
Gradle is a multi-language, multi-platform, multi-project and multi-channel build and automation
software
Gradle is an open source, advanced general purpose build management system. It is built on ANT,
Maven, and lvy repositories. It supports Groovy based Domain Specific Language (DSL) over XML.
Advantages of Gradle
Gradle will provide the following advantages compare to Ant and Maven. That’s why all new projects are using
Gradle as Build tool.
Like Maven, Gradle is also an expressive, declarative, and maintainable build Tool.
Like Maven, Gradle also supports Dependency Management
It provides very scalable and high-performance builds.
It provides Standardized project layout and life-cycle, but full flexibility and the option to fully configure the
defaults.
It is very easy-to-use Gradle tool and very flexible to implement our own Project required custom logics.
It supports for project structures that consist of more than one project to build deliverable.
It is very easy to integrate existing Ant/Maven with Gradle
It is very easy to migrate from existing Ant/Maven to Gradle
Download Gradle:
This section describes the detailed steps to install the Gradle build tool in your machine:
Open your command line and type gradle -version to get version of Gradle that is installed on your
system.
Build Script: Gradle builds a script file for handling two things; one is projects and another one
is tasks. Every Gradle build represents one or more projects. A project represents a library JAR or a
web application or it might represent a ZIP that assembled from the JARs produced by other projects.
In simple words, a project is made up of different tasks.
A task means a piece of work which a build performs. A task might be compiling some classes,
creating a JAR, generating Javadoc, or publishing some archives to a repository. Gradle uses Groovy
language for writing scripts.
So, how are these concepts related to a Gradle build? Well, every Gradle build contains one or more
projects.
The relationships between these concepts are illustrated in the following figure:
We can configure our Gradle build by using the following configuration files:
The Gradle build script (build.gradle) specifies a project and its tasks.
The Gradle properties file (gradle.properties): It is used to configure the properties of the build.
The Gradle Settings file (gradle.settings): It is optional in a build which has only one project. If our
Gradle build has more than one projects, it is mandatory because it describes which projects
participate to our build. Every multi-project build must have a settings file in the root project of the
project hierarchy.
Build phases:
A Gradle build has three distinct phases.
Initialization
During this phase the project objects are configured. The build scripts
of all projects which are part of the build are executed. Gradle 1.4
introduced an incubating opt-in feature called configuration on
demand. In this mode, Gradle configures only relevant projects
(see the section called “Configuration on demand”).
Execution
task hello {
doLast {
println 'tutorialspoint'
}
}
Execute the following command in the command prompt. It executes the above script. You should
execute this, where the build.gradle file is stored.
You can use this feature to declare external dependencies, which you want to download from the web.
Compile − The dependencies required to compile the production source of the project.
Runtime − The dependencies required by the production classes at runtime. By default, also includes the
Test Compile − The dependencies required to compile the test source of the project. By default, it includes
Test Runtime − The dependencies required to run the tests. By default, it includes runtime and test
compile dependencies.
Declaring Your Dependencies
Repositories
While adding external dependencies. Gradle looks for them in a repository. A repository is just a
collection of files, organized by group, name and version. By default, Gradle does not define any
repositories. We have to define at least one repository explicitly. The following code snippet defines
how to define maven repository. Use this code in build.gradle file.
repositories {
mavenCentral()
}
All output files of our build are created under the build directory. This directory contains the following subdirectories which
are relevant to this blog post (there are other subdirectories too, but we will talk about them in the future):
test {
java {
srcDir 'test'
}
}
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
gradle tasks
This is a good way to get a brief overview of our project without reading the build script. If we run this
command in the root directory of our example project, we see the following output:
:tasks
------------------------------------------------------------
------------------------------------------------------------
Build tasks
-----------
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build tasks
-----------
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
-----------------
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
dependencyInsight - Displays the insight into a specific dependency in root project 'first
java-project'.
Verification tasks
------------------
Rules
-----
BUILD SUCCESSFUL
Let’s move on and find out how we can package our Java project.
If the run the command gradle assemble at command prompt, we see the following output:
:compileJava
:processResources
:classes
:jar
:assemble
BUILD SUCCESSFUL
If we run the command gradle build at command prompt, we see the following output:
:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE
BUILD SUCCESSFUL
The outputs of these commands demonstrate that the difference of these tasks is that:
The assemble task runs only the tasks which are required to package our application.
The build task runs the tasks which are required to package our application AND runs automated
tests.
Both of these commands create the first-java-project.jar file to the build/libs directory.
The default name of the created jar file is created by using the following template: [project name].jar, and
the default name of the project is the same than the name of the directory in which it is created. Because
the name of our project directory is firstgradle project, the name of created jar is firstgradle.jar.
We can now try to run our application by using the following command:
The problem is that we haven’t configured the main class of the jar file in the manifest file. Let’s find out
how we can fix this problem.
apply plugin:’java’
jar
{
manifest
{
attributes 'Main-Class':
'com.cmsedge.firstgradle.HomeController'
}
After we have created a new jar file by running either the gradle jar or gradle build command, we can run
the jar file by using the following command