Gradle's purpose is to solve the problem of building and deploying the application once we've written it.
Gradle helps us do that in a consistent and automated way that's going to run on our server or mobile devices.
The particular things that Gradle reallly help us are:
While ANT and Maven use XML to describe the build file, Gradle use GROOVY, which is a variant of Java.
A Plugin is a set of tasks that allows us to build a Java project. Without the plugin, Gradle does not know how to build the project.
We can think Gradle as a worker but he needs guidelines to build a house. In this sense, these guidelines are plugins. And a guidelines can contain many actions, which can be thought that a plugin can contain many tasks.
Furthermore, the worker can share the guidelines to other workers. Hence, a Gradle plugin can be used across many projects.
Below, I show some common Gradle tasks. More tasks are shown later.
I will introduce more about Plugin in Section 5.3. It is OK that we can skip it for a while before coming back.
We need to create a 'build.gradle' file in the same directory level with 'src'.
Right click in build.gradle file, select Import Gradle Project
Inside build.gradle, we declare 4 things like this:
After that, we open a Terminal (cmd) inside the project folder, and run:
gradle build
Gradle now executes all tasks inside 'java' plugin, and prints out the result of the build: BUILD SUCCESSFUL
After a successful build, Gradle generates a build folder in our project, which contains those mentioned artifacts.
Besides, these is a JAR artifact. It is assembled as a result of the java plugin assemble task.
This is how Gradle deploys our project. We can ship this JAR artifact to our server and deploy it.
Before moving on, let's explain line 10 of the build file above.
compile: 'com.google.code.gson:gson:2.8.0'
compile
- configuration name (to be discussed)com.google.code.gson:gson:2.8.0
- Library specification, containing 3 parts separated by semicolonsWhat does Gradle Wrapper do?
Why Gradle Wrapper is important? Because the build instruction may vary across Gradle versions.
How can you know if you have Gradle Wrapper? If you have all these things:
+gradlew (Linux) or gradlew.bat (Windows)
+gradle folder, containing
+wrapper
+gradle-wrapper.jar, and
+gradle-wrapper.properties
If you have none of them, you can create the wrapper by running:
gradle wrapper --gradle-version 5.4.1 (or different versions)
Note: the requested version of wrapper is specified in the file gradle-wrapper.properties, in the value of distributionUrl
Everything inside build.gradle is relavant to Project object. For example,
sourceCompatibility=1.8
is actually an assigning operation of project.sourceCompatibility=1.8,
similar to dependencies, repository, etc...
settings.gradle : for declaring the rootProject, and include sub projects (SubModules)
// settings.gradle
rootProject.name = "java-project"
include "sub-project-1"
include "sub-project-2"
After executing a task, in console, it lists all tasks which are executed just before running the requested task.
For example, if you execute gradle assemble --console=verbose
, it shows
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar UP-TO-DATE
> Task :assemble UP-TO-DATE (the last requested task)
Another example: gradle build --console=verbose
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar UP-TO-DATE
> Task :assemble UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build UP-TO-DATE
In build.gradle, create a new task to print out the current date, like this
task showDate{
doLast {
println "Current Date" + new Date()
}
dependsOn(assemble)
group = "My tasks"
description = "Show current date"
}
In which,
doLast { ... } : perform particular actions.
dependsOn(assemble): if you run in terminal 'gradle showDate', it will run assemble task before executing showDate
group: group whose this task is in
As I mention earlier in Section 2, a plugin contains a set of tasks.
Plugin is a JAR file. And it is also included to the project. At this point, you may think that Plugin is same as Dependency.
It is both True and False.
It is true because it is a library that something rely on.
However, it is false because it is not a dependency on the project source code, this is a dependency for the build script itself