Gradle Introduction
Gradle is an open-source build automation tool focused on flexibility and performance.
Installation
see Installing Gradle for detailed instruction
If you are using sdkman, just run
1 | sdk install gradle |
It will ask you to set the default gradle, you can just set the latest version.
To get a list of gradle candidate versions
1 | sdd list gradle |
For Linux user, Just download gradle binary from https://gradle.org/install/ and add its bin folder to System path.
To test if Gradle is installed, get its version:
1 | gradle -v |
gradle -v
command will show the JVM version. JVM version is set from JAVA_HOME Environment Variable.
Eclipse Integration - Gradle has an official Eclipse plugin called BuildShip.
You can learn Gradle from the official document - Learning the Basics section.
Projects, plugins and tasks
Gradle build is made up of one or more projects. Gradle projects are made up of one or more tasks that perform build steps. Most of the time, a project has a build.gradle file.
A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.
Typically, tasks are provided by applying a plugin so that you do not have to define them yourself.
First Build Script
build.gradle file contains the build script. It defines how the project is build. build.gradle file can have tasks, dependencies and plugins. Here we define a task called hello
.
build.gradle
1 | task hello { |
You can use Groovy or Kotlin to write Gradle build script. Here we use Groovy.
To run a build, run gradle <task>
. Here we pass the name of the task ‘hello’ as argument.
1 | $ gradle hello |
You can add -q
option to hide the extra information.
1 | $ gradle -q hello |
use gradle tasks --all
command to display all tasks
Extra Property
Additional, ad-hoc, properties for Gradle domain objects.
To define a foo property with value “bar”. You can use dollar sign to get the value of a variable. So to access property value foo, use $foo
or ${foo}
.
1 | task hello { |
You can also define property like this
1 | ext { |
Example - Use of property when defining spring dependencies
1 | ext { |
REMEMBER: To refer to a property in a string. The String MUST be enclosed with double quotes. Not single quotes!
see Extra Properties for more information
Default Task
Gradle allows you to define one or more default tasks that are executed if no other tasks are specified.
1 | defaultTasks 'clean', 'run' |
Project
Each build script you have is associated with an object of type Project and as the build script executes, it configures this Project
Project has many standard properties. You can find all properties from https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#N15101
Example to set group, version and sourceCompatibility property of Project.
1 | name = 'projectname' |
You can reference the project property using $, just like extra property. Example to print project name and version:
1 | task projectinfo { |
You can define extra properties for the project through ext
namespace.
1 | project.ext.prop1 = "foo" |
Plugin
Gradle provides very little features for real world automation. All of the useful features like compile java code are added by plugins.
syntax
1 | plugins { |
Applying plugins to a project.
1 | plugins { |
To learn more about plugins see - Using Gradle Plugins.
Core plugins are provided by Gradle.
Base Plugin
Base Plugin defines serveral standard lifecycle tasks. All the core language plugins, like the Java plugin, apply the Base Plugin and inherit these lifecycle tasks.
Tasks
clean
Deletes the build directory and everything in it
1 | gradle clean |
check
verification tasks should be attached to check lifecycle task
assemble
Artifact related tasks such as jar
should be attached to this lifecycle task
build
Intended to build everything, including running all tests, producing the production artifacts and generating documentation. You will use gradle build
command a lot to build project.
1 | gradle build |
Java Plugin
Java Plugin is the most used core plugin. The Java plugin adds Java compilation along with testing and bundling capabilities to a project. It serves as the basis for many of the other JVM language Gradle plugins.
some useful tasks Java Plugin uses are compileJava, compileTestJava, jar, javadoc, test, clean…
The Java plugin attaches some of its tasks to the lifecycle tasks defined by the Base Plugin and adds a few other lifecycle task
add java plugin
1 | plugins { |
To build java project, just need to run gradle build
1 | gradle build |
Repositories
Maven Central is a popular repository hosting open source libraries for consumption by Java projects.
To declare the Maven Central repository for your build
1 | repositories { |
To declare JCenter Maven Repository
1 | repositories { |
For more on Repositories see Declaring Repositories
Adding Dependencies
Example to add dependencies
1 | dependencies { |
The default dependency configurations are
- Compile
- Runtime
- Test Compile
- Test Runtime
Bootstrapping new Project
Use built-in gradle init
task to create a new Gradle build
1 | gradle init |
You can also specify a project type
1 | gradle init --type basic |
Clear Cache
Gradle stores all dependencies in caches folder. This folder can get very big after a while. To remove cache, run the following command.
1 | rm -r $HOME/.gradle/caches/ |
Update Gradle Wrapper
To update the Gradle wrapper, run the following command. It will update the gradle wrapper to the latest version.
1 | gradle wrapper --gradle-version 8.6 |