If you’ve worked in Java, you probably have encountered Gradle by now, be it in an Android or a Spring Application. Whether you have used Gradle or not, It’s good to know one of the leading build tools in the market.
Gradle is the next evolutionary step in JVM-based build tools. It draws on lessons learned from established tools such as Ant and Maven and takes their best ideas to the next level. Following a build-by-convention approach, Gradle allows for declaratively modelling your problem domain using a powerful and expressive domain-specific language (DSL) implemented in Groovy instead of XML. Because Gradle is a JVM native, it allows you to write custom logic in the language you’re most comfortable with, be it Java or Groovy.
- Gradle builds are not just for Java applications. You can leverage Gradle to help you build applications in Java, C++, Python or your language of choice.
To install Gradle, please follow the official docs to get up to date installation instructions. Installation using package manager is recommended.
- A Gradle Build can have multiple Projects which in turn can have multiple Tasks
gradle.propertiesfile is required to configure the build and
settings.gradlefile is required for multi-project builds to tell Gradle which projects to build.
- Gradle supports Plugins, which contains dependencies, tasks and configurations(like source location) that can be applied to our build and make Gradle easy to use.
Check out the categorized list of features provided by Gradle.
- Incremental Builds
- Parallel Execution
- Continuous Execution even After Failures
- Transitive Dependencies
- Version Conflict Resolution
- Gradle Features
- Customizable Logging
Building a Java Application using Gradle
- Gradle comes with a built-in plugin called the Build Init Plugin. You can see the details of building a Java application in the official docs.
- It generates the default
testfolders for the source code along with other configurational files.
build.gradlefile contains java and application plugins, a class name for the entry point of jar, the public Bintray Artifactory repository and some dependencies to help with the development and testing of the application.
- Because the Gradle build used the Application plugin, you can use the
./gradlew runcommand to Build, Test and Execute the project.
Important components of Gradle
This is the recommended way to build a Gradle project. When you initialize a Gradle project, the gradle wrapper is set up automatically which can be executed just like Gradle as:
Gradle changes a lot with its upgrades and the old
gradle.build files might not work for the newer versions. Gradle Wrapper automatically downloads and uses the recommended version of Gradle to execute your tasks.
You have to push the Gradle wrapper files to Version control like git, including jars, so everyone can use the same wrapper to build your projects You can upgrade the Gradle wrapper via it’s CLI to make sure any other optimizations present in the build are also applied.
- This daemon runs in the background on your system and helps builds run faster by avoiding the expensive bootstrapping process as well as leveraging caching, by keeping data about your project in memory.
- Gradle Daemon is enabled by default starting with Gradle 3.0, so it requires no setup.
- In between builds, it waits idly for the next build. This has the obvious benefit of only requiring Gradle to be loaded into memory once for multiple builds, as opposed to once for each build.
- The Daemon can also leverage caching and can progressively become more optimal with each rebuild to decrease the build times.
Go through the Gradle CLI Docs to see what else you can do;
Dependency management with Gradle
This is one of the major feature provided by a build system. Dependency management is a technique for declaring, resolving and using dependencies required by the project in an automated fashion.
There are some widely used public repositories of Gradle like
google(). But you can also define other repositories by url, even the ones secure access via credentials.
There are different types of dependencies:
As Gradle is widely used in Java and Android projects, you can check these links to get a detailed overview. They will give you an idea on how to use compile, runtime or test dependencies among other things: