The Gradle version is incompatible with the idea version
Build Tools
Advantage
Disadvantage
Ant
Flexible to use and faster than Maven and gradle
Without imposing any coding convention on the project directory structure, developers need to write complex XML file build instructions, which is a challenge for developers.
Maven
Follow a set of project directory structures with conventions greater than configuration, use agreed GAV coordinates for dependency management, and focus on package management.
The project construction process is rigid, configuration file writing is not flexible enough, it is inconvenient to customize components, and the construction speed is slower than Gradle
Gradle
It combines the flexibility of Ant scripts + the advantages of Maven conventions over configured project directories, supports a variety of remote warehouses and plug-ins, and focuses on the construction of large projects.
The learning cost is high, there is little information, the script is flexible, and the version compatibility is poor.
1. Install Gradle
Windows
First go to the C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.3\plugins\gradle\lib directory to confirm the gradle version compatible with the current Idea. At least version 8.4 of gradle must be installed here.
URL: https://gradle.org/releases/ →Select the corresponding binary version to download. The bin directory of the complete version may not have a startup command.
Configure environment variables My Computer → Properties → Advanced System Settings → Environment Variables
C:\Users\ArtistS>gradle -v
Welcome to Gradle 8.6!
Here are the highlights of this release:
- Configurable encryption key for configuration cache
- Build init improvements
- Build authoring improvements
For more details see https://docs.gradle.org/8.6/release-notes.html
------------------------------------------------------------
Gradle 8.6
------------------------------------------------------------
Build time: 2024-02-02 16:47:16 UTC
Revision: d55c486870a0dc6f6278f53d21381396d0741c6e
Kotlin: 1.9.20
Groovy: 3.0.17
Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM: 17.0.10 (Oracle Corporation 17.0.10+11-LTS-240)
OS: Windows 11 10.0 amd64
Configure the gradle local warehouse, My Computer → Properties → Advanced System Settings → Environment Variables, add the following configuration
artists@Artists-MacBook-Pro ~ % gradle -v
Welcome to Gradle 8.7!
Here are the highlights of this release:
- Compiling and testing with Java 22
- Cacheable Groovy script compilation
- New methods in lazy collection properties
For more details see https://docs.gradle.org/8.7/release-notes.html
2. Gradle project directory structure
The default directory structure of the Gradle project is consistent with that of the Maven project, both based on Convention Over Configuration. The complete project directory structure is as follows:
Gradle Directory
Maven Directory
build → Encapsulates compiled bytecode, packaged [Jar, War], test report and other information
target → The compiled classes files will be stored inside.
src → To put source code
src → To put source code
settings.gradle → Settings file, defines project and sub-project name information, and has a one-to-one correspondence with the project
ㅤ
build.gradle → Build script, each Project (can be understood as a maven module) has a build.gradle
pom.xml → Build script, store dependencies
Tip: gradlew.bat and gradlew execute the gradle instructions in the wrapper version specified in gradle, not the locally installed gradle instructions. So if you only compile locally, there is no need for these three folders to exist.
Specify console mode via Gradle properties. Different modes are described immediately below.
ㅤ
--build-cache,--no-build-cache
Try to reuse output from previous version. The default is off.
ㅤ
--max-workers
Set usable workers of Gradle. The default is the number of processors.
ㅤ
--parallel, --no-parallel
Parallel execute project. The default is off.
ㅤ
--daemon,--no-daemon
Use daemon to build. The default is on.
ㅤ
--foreground
Start Gradle daemon in the foreground process.
ㅤ
5. init.d Folder
5.1 init.d folder
We can create xxx.gradle file in C:\Software\gradle-8.6-bin\gradle-8.6\init.d, xxx.gradle file can be executed before build, so you can config some preload operations.
5.2 Create init.gradle in init.d
// all projects will use the following configuration
allprojects{
/*
Dependencies require for the project will download from
the following repositories
*/
repositories{
/*
It will try to find the dependencies in maven local
repository, this need M2_HOME environment variable
*/
mavenLocal()
// Third-party repository
maven { name "Alibaba" ; url"https://maven.aliyun.com/repository/public"}
// Third-party repository
maven { name "Bstek" ; url"https://nexus.bsdn.org/content/groups/public"}
}
/*
Use for build.gradle build script(e.g. plugins), if these scripts
need some dependencies, it will download from the
following repositories.
*/
buildscript{
maven { name "Alibaba" ; url"https://maven.aliyun.com/repository/public"}
maven { name "Bstek" ; url"https://nexus.bsdn.org/content/groups/public"}
maven { name "M2" ; url'https://plugins.gradle.org/m2/'}
}
}
5.3 How to enable init.gradle?
If there are more than 2 of the following methods, gradle will follow the order to execute them. If there are more than 2 init script under same folder, gradle will execute them in the order of a-z. Each init script will has a gradle instance, the methods and properties you called in the init script, will delegate to this gradle instance.
Use command line
# You can enter this command multiple times to specify multiple files
gradle --init-script [DIR_PATH]/init.gradle -q [TASK_NAME]
Putinit.gradlefile into [USER_HOME]/.gradle/
e.g. C:\Users\ArtistS\.gradle
Put xxx.gradle into [USER_HOME]/.gradle/init.d/
Put xxx.gradle into [GRADLE_HOME]/init.d/
e.g. C:\Software\gradle-8.6-bin\gradle-8.6\init.d
5.4 Repository instructions
mavenLocal() → Gradle will find the repository by the repository path in maven settings.xml. The order in which gradle searches for jar packages is as follows:
[USER_HOME]/.m2/settings.xml → [M2_HOME]/conf/settings.xml → [USER_HOME]/.m2/repository
maven{[URL address]} → e.g. private repository, alibaba repository
mavenCentral() → Maven central repository, no need to config, you can use it by directly declaring it
Gradle can avoid downloading from the remote repository every time by combining the specified repository and remote repository. But here is a problem, if the local maven repository has this dependency, gradle will load it directly. But if the local maven doesn’t have this dependency, Gradle will download it from a remote repository. Keep in mind, that this jar download from the remote repository will not be stored in the maven repository, it will be put into the cache directory, the default path is [USER_HOME]/.gradle/cashes. If you didn’t configure the GRADLE_USER_HOME environment variable. It will be put into [GRADLE_USER_HOME]/cashes. There is no other way to put the downloaded jar into Maven repository, because the format of the jar downloaded in cashes folder is different from the jar stored in the maven repository.
Gradle Wrapper is a layer of packaging for gradle, it used to solve the problem different projects need different Gradle version. In fact, after having gradle wrapper, we don’t need to configure the Gradle anymore, we can use the gradle project’s wrapper to operate it.
E.g. I want to share my code to you, there will be 2 scenarios happen
There is no Gradle on your computer.
Your computer has Gradle, but the version too old.
6.1 How to use Gradle wrapper?
gradelw, gradlew.cmd uses the version specified by gradle wrapper. Because, we will use local Gradle in most cases, so the local Gradle command may be different from Gradle Wrapper command.
# You can compare the result of these 2 command in your project
gradle -v
gradlew -v
gradlew.bat -v
But the usage of gradle and gradlew is the same.
6.2 How to change gradle wrapper version?
We can use some parameters to control the generation of Wrapper.
# You can check the gradle wrapper version in gradle-wrapper.properties
# You can use this way to upgrade the gradle wrapper (only change the version in
# gradle-wrapper.properties but it doesn't download yet.)
gradle wrapper --gradle-version=[version number]
gradle wrapper --gradle-verison=7.4.2
The above operation can only change the version in gradle-wrapper.properties. When gradle download the new version?
Gradle Wrapper Implementation Process:
When we first execute ./gradlew build , gradlew will read gradle-wrapper.properties
Gradle will download the specific version and put it into [GRADLE_HOME]/wrapper/dists
Build local cash, put it into [GRADLE_HOME]/cashes. If the version you want to download already in this folder, you don’t need to download it anymore.
After that, all ./gradlew will use this specific Gradle version.
6.3 gradle-wrapper.properties
Field
Description
distributionBase
The storage directory after decompression of the gradle compression package
distributionPath
The path of gradle compression package, after decompression of the gradle compression package.
zipStoreBase
Same as distributionBase, but this is for zip package.
zipStiorePath
Same as distributionPath, but this is for zip package.
distributionUrl
Download address of gradle distribution compressed package
6.4 When we use gradle, when we use gradlew
If the project you wrote before or this project you copy or share from someone, you should use gradlew. But if you create a new project with Gradle, you should use gradle rather than gradlew.
7. Create & Deploy Project in IDEA
7.1 Create general JAVA project with Gradle by IDEA
New Project → Update details as the following screenshot
After create project, the project will use IDEA gradle version, not your local gradle version. If you want to change gradle to your local gradle, File → Settings → Build,Execution,Deployment → Build Tools → Gradle
Tip:
1. You can only use this way to change Gradle to your local Gradle when you create a new project.
2. When you add dependencies on gradle.build, these dependencies will download to [GRADLE_USER_HOME]/cashes/module-2/files-2.1
Tip: No matter Junit 4.x or Junit 5.x, we only need to run gradle test under build.gradle, and Gradle will help use to run all the test with @Test, and generate test report.
// If you don't want to run test you can run the following command
gradle build -x
// Or you can configure in the gradle.build file
test{
enabled(false)
useJUnitPlatform() // Support for Junit5
}
// If you only want to execute the test under a specific package
test{
enabled(true)
useJUnitPlatform() // Support for Junit5
include('com/artists/**') // It will only run the test under com.artists package
}
// If you don't want to execute the test under a specific package
test{
enabled(true)
useJUnitPlatform() // Support for Junit5
exclude('com/artists/**') // It will only run the test exclude the test under com.artists package
}
9. Gradle Life Cycle
3 stages of Gradle projects:
Initialization
- Detects the settings.gradle(.kts) file.
- Creat a Settings instance.
- Evaluates the settings file to determine which projects (and included builds) make up the build.
- Creates a Project instance for every project.
Configuration
- Evaluates the build scripts, build.gradle(.kts), of every project participating in the build.
- Creates a task graph for requested tasks.
Execution
- Schedules and executes the selected tasks.
- Dependencies between tasks determine execution order.
- Execution of tasks can occur in parallel.
Start from [GRADEL_USER_HOME]/init.gradle(init script)
Find settings.gradle, this will record project name, sub project name. Use this project name to find each project.
To execute each build.gradle in each project.(The priority is the build.gradle in parent project will be executed first, and then the sub project will be executed)
During the Configuration stage, it will load all Build Script of all module. The “load” means all statements in build.gradle. Gradle will create corresponding tasks based on the code, and generate DAF consisting of Task.
10. settings.gradle
A project is composed of one or more subprojects (sometimes called modules).
Gradle reads the settings.gradle(.kts) file to figure out which subprojects comprise a project build. Each project can only have 1 settings.gradle.
Example:
Create a new project named gradle-root
Create 2 subprojects
Check the settings.gradle, it will looks like
rootProject.name = 'gradle-root'
include 'sub-project01'
include 'sub-project02'
include 'sub-project03'
/**
* The configuration can also be
* include(':sub-project01')
* : -> separator, like / in the path e.g. /root/sub-project01
* '[relative-path]' -> base on the root project
*
* If you create a sub project of a sub project, the
* configuration should like
* include 'sub-project01:sub-sub-project01'
*/
11. Task
A task represents some independent unit of work that a build performs, such as compiling classes, creating a JAR, generating Javadoc, or publishing archives to a repository.
All available tasks in your project come from Gradle plugins and build scripts. You can list all the available tasks in the project by running the following command in the terminal: gradlew tasks
Here is a sample task @ArtistS created
task("task_1", {
//This will run during the task configuration section
println("This is a simple task.")
// The following will run during the task execution section
doFirst {
println("task_1 doFirst()")
}
doLast {
println("task_2 doLast()")
}
})
// We can simplified the above code, if you don’t understand anything, you can check
// the groovy documentation.
task task_1 {
//This will run during the task configuration section
println "This is a simple task."
// The following will run during the task execution section
doFirst {
println "task_1 doFirst()"
}
doLast {
println "task_2 doLast()"
}
}
And go to the root path of gradle-root which is C:\GitRepository\Daydayup\Gradle\gradle-root, and then run gradle -i task_1 . The log is following, you can see when it prints the red context. (If you can’t understand, back to the life cycle section)
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle -i task_1
Initialized native services in: C:\GradleRepository\native
Initialized jansi services in: C:\GradleRepository\native
Received JVM installation metadata from 'C:\Software\jdk17': {JAVA_HOME=C:\Software\jdk17, JAVA_VERSION=17.
0.10, JAVA_VENDOR=Oracle Corporation, RUNTIME_NAME=Java(TM) SE Runtime Environment, RUNTIME_VERSION=17.0.10
+11-LTS-240, VM_NAME=Java HotSpot(TM) 64-Bit Server VM, VM_VERSION=17.0.10+11-LTS-240, VM_VENDOR=Oracle Corporation, OS_ARCH=amd64}
Removing 1 daemon stop events from registry
Starting a Gradle Daemon (subsequent builds will be faster)
Starting process 'Gradle build daemon'. Working directory: C:\GradleRepository\daemon\8.6 Command: C:\Softw
are\jdk17\bin\java.exe --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNA
MED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED -
-add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=ja
va.base/java.util.concurrent.atomic=ALL-UNNAMED -XX:MaxMetaspaceSize=384m -XX:+HeapDumpOnOutOfMemoryError -
Xms256m -Xmx512m -Dfile.encoding=GBK -Duser.country=CN -Duser.language=zh -Duser.variant -cp C:\Software\gr
adle-8.6-bin\gradle-8.6\lib\gradle-launcher-8.6.jar -javaagent:C:\Software\gradle-8.6-bin\gradle-8.6\lib\agents\gradle-instrumentation-agent-8.6.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.6
Successfully started process 'Gradle build daemon'
An attempt to start the daemon took 1.201 secs.
The client will now receive all logging from the daemon (pid: 53944). The daemon log file: C:\GradleRepository\daemon\8.6\daemon-53944.out.log
Starting build in new daemon [memory: 512 MiB]
Using 24 worker leases.
Received JVM installation metadata from 'C:\Software\jdk17': {JAVA_HOME=C:\Software\jdk17, JAVA_VERSION=17.0.10, JAVA_VENDOR=Oracle Corporation, RUNTIME_NAME=Java(TM) SE Runtime Environment, RUNTIME_VERSION=17.0.10
+11-LTS-240, VM_NAME=Java HotSpot(TM) 64-Bit Server VM, VM_VERSION=17.0.10+11-LTS-240, VM_VENDOR=Oracle Corporation, OS_ARCH=amd64}
Watching the file system is configured to be enabled if available
Now considering [C:\GitRepository\Daydayup\Gradle\gradle-root] as hierarchies to watch
File system watching is active
Starting Build
Compiling settings file 'C:\GitRepository\Daydayup\Gradle\gradle-root\settings.gradle' using SubsetScriptTransformer.
Compiling settings file 'C:\GitRepository\Daydayup\Gradle\gradle-root\settings.gradle' using BuildScriptTransformer.
Settings evaluated using settings file 'C:\GitRepository\Daydayup\Gradle\gradle-root\settings.gradle'.
Projects loaded. Root project using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'
Included projects: [root project 'gradle-root', project ':sub-project01', project ':sub-project02', project ':sub-project03']
> Configure project :
Evaluating root project 'gradle-root' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle' using SubsetScriptTransformer.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle' using BuildScriptTransformer.
This is a simple task.
> Configure project :sub-project01
Evaluating project ':sub-project01' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project01\build.gradle'.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project01\build.gradle' using SubsetScriptTransformer.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project01\build.gradle' using BuildScriptTransformer.
> Configure project :sub-project02
Evaluating project ':sub-project02' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project02\build.gradle'.
> Configure project :sub-project03
Evaluating project ':sub-project03' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project03\build.gradle'.
All projects evaluated.
Task name matched 'task_1'
Selected primary task 'task_1' from project :
Tasks to be executed: [task ':task_1']
Tasks that were excluded: []
Resolve mutations for :task_1 (Thread[Execution worker Thread 2,5,main]) started.
:task_1 (Thread[included builds,5,main]) started.
> Task :task_1
Caching disabled for task ':task_1' because:
Build cache is disabled
Task ':task_1' is not up-to-date because:
Task has not declared any outputs despite executing actions.
task_1 doFirst()
task_1 doLast()
BUILD SUCCESSFUL in 4s
1 actionable task: 1 executed
Watched directory hierarchies: []
11.1 Task Behavior
This section talks about action execution order. Totally speaking, there is an action list, doFirst() will be put at the start of this action list, and doLast() will be put at the end of this action list. Between these 2 task behaviors is self-action. You may saw some code like
task << {
println("<< means doLast() only can use in 5.x, this already deprecated now")
}
Task behavior can also be declared out of the task code block.
task("task_1", {
//This will run during the task configuration section
println("This is a simple task.")
// The following will run during the task execution section
doFirst {
println("task_1 doFirst()")
}
doLast {
println("task_1 doLast()")
}
})
task_1.doFirst{
println("task_1 doFirst() outer")
}
task_1.doLast{
println("task_1 doLast() outer")
}
I will run the gradle -i task_1 under the root path of gradle-path. The log is here.
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle -i task_1
Initialized native services in: C:\GradleRepository\native
Initialized jansi services in: C:\GradleRepository\native
Received JVM installation metadata from 'C:\Software\jdk17': {JAVA_HOME=C:\Software\jdk17, JAVA_VERSION=17.0.10, JAVA_VENDOR=Oracle Corporation, RUNTIME_NAME=Java(TM) SE Runtime Environment, RUNTIME_VERSION=17.0.10+11-LTS-240, VM_NAME=Java HotSpot(TM) 64-Bit Server VM, VM_VERSION=17.0.10+11-LTS-240, VM_VENDOR=Oracle Corporation, OS_ARCH=amd64}
The client will now receive all logging from the daemon (pid: 53944). The daemon log file: C:\GradleRepository\daemon\8.6\daemon-53944.out.log
Starting 2nd build in daemon [uptime: 7 mins 25.741 secs, performance: 100%, GC rate: 0.00/s, heap usage: 0% of 512 MiB, non-heap usage: 13% of 384 MiB]
Using 24 worker leases.
Now considering [C:\GitRepository\Daydayup\Gradle\gradle-root] as hierarchies to watch
Watching the file system is configured to be enabled if available
File system watching is active
Starting Build
Settings evaluated using settings file 'C:\GitRepository\Daydayup\Gradle\gradle-root\settings.gradle'.
Projects loaded. Root project using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
Included projects: [root project 'gradle-root', project ':sub-project01', project ':sub-project02', project ':sub-project03']
> Configure project :
Evaluating root project 'gradle-root' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle' using SubsetScriptTransformer.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle' using BuildScriptTransformer.
This is a simple task.
> Configure project :sub-project01
Evaluating project ':sub-project01' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project01\build.gradle'.
> Configure project :sub-project02
Evaluating project ':sub-project02' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project02\build.gradle'.
> Configure project :sub-project03
Evaluating project ':sub-project03' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project03\build.gradle'.
All projects evaluated.
Task name matched 'task_1'
Selected primary task 'task_1' from project :
Tasks to be executed: [task ':task_1']
Tasks that were excluded: []
Resolve mutations for :task_1 (Thread[Execution worker,5,main]) started.
:task_1 (Thread[Execution worker,5,main]) started.
> Task :task_1
Caching disabled for task ':task_1' because:
Build cache is disabled
Task ':task_1' is not up-to-date because:
Task has not declared any outputs despite executing actions.
task_1 doFirst() outer
task_1 doFirst()
task_1 doLast()
task_1 doLast() outer
BUILD SUCCESSFUL in 976ms
1 actionable task: 1 executed
Watched directory hierarchies: []
Case 2:
def map = new HashMap<String,Object>()
map.put("action",{println("task_1 action in map()")})
task(map,"task_1", {
//This will run during the task configuration section
println("This is a simple task.")
// The following will run during the task execution section
doFirst {
println("task_1 doFirst()")
}
doLast {
println("task_1 doLast()")
}
})
task_1.doFirst{
println("task_1 doFirst() outer")
}
task_1.doLast{
println("task_1 doLast() outer")
}
Log is here
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle -i task_1
Initialized native services in: C:\GradleRepository\native
Initialized jansi services in: C:\GradleRepository\native
Received JVM installation metadata from 'C:\Software\jdk17': {JAVA_HOME=C:\Software\jdk17, JAVA_VERSION=17.0.10, JAVA_VENDOR=Oracle Corporation, RUNTIME_NAME=Java(TM) SE Runtime Environment, RUNTIME_VERSION=17.0.10+11-LTS-240, VM_NAME=Java HotSpot(TM) 64-Bit Server VM, VM_VERSION=17.0.10+11-LTS-240, VM_VENDOR=Oracle Corporation, OS_ARCH=amd64}
The client will now receive all logging from the daemon (pid: 53944). The daemon log file: C:\GradleRepository\daemon\8.6\daemon-53944.out.log
Starting 4th build in daemon [uptime: 1 hrs 8 mins 18.674 secs, performance: 100%, GC rate: 0.00/s, heap usage: 0% of 512 MiB, non-heap usage: 13% of 384 MiB]
Using 24 worker leases.
Now considering [C:\GitRepository\Daydayup\Gradle\gradle-root] as hierarchies to watch
Watching the file system is configured to be enabled if available
File system watching is active
Starting Build
Settings evaluated using settings file 'C:\GitRepository\Daydayup\Gradle\gradle-root\settings.gradle'.
Projects loaded. Root project using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
Included projects: [root project 'gradle-root', project ':sub-project01', project ':sub-project02', project ':sub-project03']
> Configure project :
Evaluating root project 'gradle-root' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
This is a simple task.
> Configure project :sub-project01
Evaluating project ':sub-project01' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project01\build.gradle'.
> Configure project :sub-project02
Evaluating project ':sub-project02' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project02\build.gradle'.
> Configure project :sub-project03
Evaluating project ':sub-project03' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project03\build.gradle'.
All projects evaluated.
Task name matched 'task_1'
Selected primary task 'task_1' from project :
Tasks to be executed: [task ':task_1']
Tasks that were excluded: []
Resolve mutations for :task_1 (Thread[Execution worker,5,main]) started.
:task_1 (Thread[Execution worker,5,main]) started.
> Task :task_1
Caching disabled for task ':task_1' because:
Build cache is disabled
Task ':task_1' is not up-to-date because:
Task has not declared any outputs despite executing actions.
task_1 doFirst() outer
task_1 doFirst()
task_1 action in map()
task_1 doLast()
task_1 doLast() outer
BUILD SUCCESSFUL in 874ms
1 actionable task: 1 executed
Watched directory hierarchies: []
11.2 Understanding Dependencies Between Tasks
Many times, a task requires another task to run first. If task B uses the output of task A, then task A must complete before task B begins. There are 4 types of dependencies
Parameters Dependencies
// Task Dependencies
task A {
doLast {
println "This is TaskA"
}
}
task B {
doLast {
println "This is TaskB"
}
}
task C(dependsOn:['A','B']){
doLast {
println "This is TaskC"
}
}
The log is here
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle -i C
Initialized native services in: C:\GradleRepository\native
Initialized jansi services in: C:\GradleRepository\native
Received JVM installation metadata from 'C:\Software\jdk17': {JAVA_HOME=C:\Software\jdk17, JAVA_VERSION=17.0.10, JAVA_VENDOR=Oracle Corporation, RUNTIME_NAME=Java(TM) SE Runtime Environment, RUNTIME_VERSION=17.0.10
+11-LTS-240, VM_NAME=Java HotSpot(TM) 64-Bit Server VM, VM_VERSION=17.0.10+11-LTS-240, VM_VENDOR=Oracle Corporation, OS_ARCH=amd64}
Removing daemon from the registry due to communication failure. Daemon information: DaemonInfo{pid=53944, a
ddress=[8940263b-6149-4fed-b7da-0059f87154ae port:9718, addresses:[/127.0.0.1]], state=Idle, lastBusy=17166
69901931, context=DefaultDaemonContext[uid=88fcf08a-0743-4169-b98a-1276c7482da8,javaHome=C:\Software\jdk17,
daemonRegistryDir=C:\GradleRepository\daemon,pid=53944,idleTimeout=10800000,priority=NORMAL,applyInstrument
ationAgent=true,daemonOpts=--add-opens=java.base/java.util=ALL-UNNAMED,--add-opens=java.base/java.lang=ALL-
UNNAMED,--add-opens=java.base/java.lang.invoke=ALL-UNNAMED,--add-opens=java.prefs/java.util.prefs=ALL-UNNAM
ED,--add-opens=java.base/java.nio.charset=ALL-UNNAMED,--add-opens=java.base/java.net=ALL-UNNAMED,--add-open
s=java.base/java.util.concurrent.atomic=ALL-UNNAMED,-XX:MaxMetaspaceSize=384m,-XX:+HeapDumpOnOutOfMemoryError,-Xms256m,-Xmx512m,-Dfile.encoding=GBK,-Duser.country=CN,-Duser.language=zh,-Duser.variant]}
Removing 0 daemon stop events from registry
Previous Daemon (53944) stopped at Sun May 26 11:33:00 BST 2024 by user or operating system
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
Starting process 'Gradle build daemon'. Working directory: C:\GradleRepository\daemon\8.6 Command: C:\Software\jdk17\bin\java.exe --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNA
MED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED -
-add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=ja
va.base/java.util.concurrent.atomic=ALL-UNNAMED -XX:MaxMetaspaceSize=384m -XX:+HeapDumpOnOutOfMemoryError -
Xms256m -Xmx512m -Dfile.encoding=GBK -Duser.country=CN -Duser.language=zh -Duser.variant -cp C:\Software\gr
adle-8.6-bin\gradle-8.6\lib\gradle-launcher-8.6.jar -javaagent:C:\Software\gradle-8.6-bin\gradle-8.6\lib\agents\gradle-instrumentation-agent-8.6.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.6
Successfully started process 'Gradle build daemon'
An attempt to start the daemon took 1.191 secs.
The client will now receive all logging from the daemon (pid: 55388). The daemon log file: C:\GradleRepository\daemon\8.6\daemon-55388.out.log
Starting build in new daemon [memory: 512 MiB]
Using 24 worker leases.
Received JVM installation metadata from 'C:\Software\jdk17': {JAVA_HOME=C:\Software\jdk17, JAVA_VERSION=17.0.10, JAVA_VENDOR=Oracle Corporation, RUNTIME_NAME=Java(TM) SE Runtime Environment, RUNTIME_VERSION=17.0.10
+11-LTS-240, VM_NAME=Java HotSpot(TM) 64-Bit Server VM, VM_VERSION=17.0.10+11-LTS-240, VM_VENDOR=Oracle Corporation, OS_ARCH=amd64}
Watching the file system is configured to be enabled if available
Now considering [C:\GitRepository\Daydayup\Gradle\gradle-root] as hierarchies to watch
File system watching is active
Starting Build
Settings evaluated using settings file 'C:\GitRepository\Daydayup\Gradle\gradle-root\settings.gradle'.
Projects loaded. Root project using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'
Included projects: [root project 'gradle-root', project ':sub-project01', project ':sub-project02', project ':sub-project03']
> Configure project :
Evaluating root project 'gradle-root' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle' using SubsetScriptTransformer.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle' using BuildScriptTransformer.
This is a simple task.
> Configure project :sub-project01
Evaluating project ':sub-project01' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project01\build.gradle'.
> Configure project :sub-project02
Evaluating project ':sub-project02' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project02\build.gradle'.
> Configure project :sub-project03
Evaluating project ':sub-project03' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project03\build.gradle'.
All projects evaluated.
Task name matched 'C'
Selected primary task 'C' from project :
Tasks to be executed: [task ':A', task ':B', task ':C']
Tasks that were excluded: []
Resolve mutations for :A (Thread[Execution worker,5,main]) started.
:A (Thread[Execution worker,5,main]) started.
> Task :A
Caching disabled for task ':A' because:
Build cache is disabled
Task ':A' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is TaskA
Resolve mutations for :B (Thread[Execution worker,5,main]) started.
:B (Thread[Execution worker,5,main]) started.
> Task :B
Caching disabled for task ':B' because:
Build cache is disabled
Task ':B' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is TaskB
Resolve mutations for :C (Thread[Execution worker,5,main]) started.
:C (Thread[Execution worker,5,main]) started.
> Task :C
Caching disabled for task ':C' because:
Build cache is disabled
Task ':C' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is TaskC
BUILD SUCCESSFUL in 4s
3 actionable tasks: 3 executed
Watched directory hierarchies: []
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle -i C_2
Initialized native services in: C:\GradleRepository\native
Initialized jansi services in: C:\GradleRepository\native
Received JVM installation metadata from 'C:\Software\jdk17': {JAVA_HOME=C:\Software\jdk17, JAVA_VERSION=17.0.10, JAVA_VENDOR=Oracle Corporation, RUNTIME_NAME=Java(TM) SE Runtime Environment, RUNTIME_VERSION=17.0.10+11-LTS-240, VM_NAME=Java HotSpot(TM) 64-Bit Server VM, VM_VERSION=17.0.10+11-LTS-240, VM_VENDOR=Oracle Corporation, OS_ARCH=amd64}
The client will now receive all logging from the daemon (pid: 55388). The daemon log file: C:\GradleRepository\daemon\8.6\daemon-55388.out.log
Starting 2nd build in daemon [uptime: 3 mins 2.685 secs, performance: 100%, GC rate: 0.00/s, heap usage: 0% of 512 MiB, non-heap usage: 13% of 384 MiB]
Using 24 worker leases.
Now considering [C:\GitRepository\Daydayup\Gradle\gradle-root] as hierarchies to watch
Watching the file system is configured to be enabled if available
File system watching is active
Starting Build
Settings evaluated using settings file 'C:\GitRepository\Daydayup\Gradle\gradle-root\settings.gradle'.
Projects loaded. Root project using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
Included projects: [root project 'gradle-root', project ':sub-project01', project ':sub-project02', project ':sub-project03']
> Configure project :
Evaluating root project 'gradle-root' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle' using SubsetScriptTransformer.
Compiling build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle' using BuildScriptTransformer.
This is a simple task.
> Configure project :sub-project01
Evaluating project ':sub-project01' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project01\build.gradle'.
> Configure project :sub-project02
Evaluating project ':sub-project02' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project02\build.gradle'.
> Configure project :sub-project03
Evaluating project ':sub-project03' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project03\build.gradle'.
All projects evaluated.
Task name matched 'C_2'
Selected primary task 'C_2' from project :
Tasks to be executed: [task ':A', task ':B', task ':C_2']
Tasks that were excluded: []
Resolve mutations for :A (Thread[Execution worker,5,main]) started.
:A (Thread[Execution worker Thread 13,5,main]) started.
> Task :A
Caching disabled for task ':A' because:
Build cache is disabled
Task ':A' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is TaskA
Resolve mutations for :B (Thread[Execution worker Thread 13,5,main]) started.
:B (Thread[Execution worker Thread 13,5,main]) started.
> Task :B
Caching disabled for task ':B' because:
Build cache is disabled
Task ':B' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is TaskB
Resolve mutations for :C_2 (Thread[Execution worker Thread 13,5,main]) started.
:C_2 (Thread[Execution worker Thread 13,5,main]) started.
> Task :C_2
Caching disabled for task ':C_2' because:
Build cache is disabled
Task ':C_2' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is Task C_2
BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 executed
Watched directory hierarchies: []
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle -i C_3
Initialized native services in: C:\GradleRepository\native
Initialized jansi services in: C:\GradleRepository\native
Received JVM installation metadata from 'C:\Software\jdk17': {JAVA_HOME=C:\Software\jdk17, JAVA_VERSION=17.0.10, JAVA_VENDOR=Oracle Corporation, RUNTIME_NAME=Java(TM) SE Runtime Environment, RUNTIME_VERSION=17.0.10+11-LTS-240, VM_NAME=Java HotSpot(TM) 64-Bit Server VM, VM_VERSION=17.0.10+11-LTS-240, VM_VENDOR=Oracle Corporation, OS_ARCH=amd64}
The client will now receive all logging from the daemon (pid: 55388). The daemon log file: C:\GradleRepository\daemon\8.6\daemon-55388.out.log
Starting 4th build in daemon [uptime: 5 mins 27.939 secs, performance: 100%, GC rate: 0.00/s, heap usage: 0% of 512 MiB, non-heap usage: 13% of 384 MiB]
Using 24 worker leases.
Now considering [C:\GitRepository\Daydayup\Gradle\gradle-root] as hierarchies to watch
Watching the file system is configured to be enabled if available
File system watching is active
Starting Build
Settings evaluated using settings file 'C:\GitRepository\Daydayup\Gradle\gradle-root\settings.gradle'.
Projects loaded. Root project using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
Included projects: [root project 'gradle-root', project ':sub-project01', project ':sub-project02', project ':sub-project03']
> Configure project :
Evaluating root project 'gradle-root' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
This is a simple task.
> Configure project :sub-project01
Evaluating project ':sub-project01' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project01\build.gradle'.
> Configure project :sub-project02
Evaluating project ':sub-project02' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project02\build.gradle'.
> Configure project :sub-project03
Evaluating project ':sub-project03' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project03\build.gradle'.
All projects evaluated.
Task name matched 'C_3'
Selected primary task 'C_3' from project :
Tasks to be executed: [task ':A', task ':B', task ':C_3']
Tasks that were excluded: []
Resolve mutations for :A (Thread[Execution worker,5,main]) started.
:A (Thread[Execution worker Thread 8,5,main]) started.
> Task :A
Caching disabled for task ':A' because:
Build cache is disabled
Task ':A' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is TaskA
Resolve mutations for :B (Thread[Execution worker Thread 8,5,main]) started.
:B (Thread[Execution worker Thread 8,5,main]) started.
> Task :B
Caching disabled for task ':B' because:
Build cache is disabled
Task ':B' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is TaskB
Resolve mutations for :C_3 (Thread[Execution worker Thread 8,5,main]) started.
:C_3 (Thread[Execution worker Thread 8,5,main]) started.
> Task :C_3
Caching disabled for task ':C_3' because:
Build cache is disabled
Task ':C_3' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is Task C_3
BUILD SUCCESSFUL in 887ms
3 actionable tasks: 3 executed
Watched directory hierarchies: []
Cross-project dependencies
Create a task in gradle.build of sub-project01
task cross_project_task {
doLast {
println "This is task in sub-project01"
}
}
Create a task in gradle.build of gradle-root
task cross_project_task_2 {
dependsOn(":sub-project01:cross_project_task")
doLast {
println "This is task in gradle-root"
}
}
The log is here
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle -i cross_project_task_2
Initialized native services in: C:\GradleRepository\native
Initialized jansi services in: C:\GradleRepository\native
Received JVM installation metadata from 'C:\Software\jdk17': {JAVA_HOME=C:\Software\jdk17, JAVA_VERSION=17.0.10, JAVA_VENDOR=Oracle Corporation, RUNTIME_NAME=Java(TM) SE Runtime Environment, RUNTIME_VERSION=17.0.10+11-LTS-240, VM_NAME=Java HotSpot(TM) 64-Bit Server VM, VM_VERSION=17.0.10+11-LTS-240, VM_VENDOR=Oracle Corporation, OS_ARCH=amd64}
The client will now receive all logging from the daemon (pid: 55388). The daemon log file: C:\GradleRepository\daemon\8.6\daemon-55388.out.log
Starting 6th build in daemon [uptime: 12 mins 21.522 secs, performance: 100%, GC rate: 0.00/s, heap usage: 0% of 512 MiB, non-heap usage: 13% of 384 MiB]
Using 24 worker leases.
Now considering [C:\GitRepository\Daydayup\Gradle\gradle-root] as hierarchies to watch
Watching the file system is configured to be enabled if available
File system watching is active
Starting Build
Settings evaluated using settings file 'C:\GitRepository\Daydayup\Gradle\gradle-root\settings.gradle'.
Projects loaded. Root project using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
Included projects: [root project 'gradle-root', project ':sub-project01', project ':sub-project02', project ':sub-project03']
> Configure project :
Evaluating root project 'gradle-root' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\build.gradle'.
This is a simple task.
> Configure project :sub-project01
Evaluating project ':sub-project01' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project01\build.gradle'.
> Configure project :sub-project02
Evaluating project ':sub-project02' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project02\build.gradle'.
> Configure project :sub-project03
Evaluating project ':sub-project03' using build file 'C:\GitRepository\Daydayup\Gradle\gradle-root\sub-project03\build.gradle'.
All projects evaluated.
Task name matched 'cross_project_task_2'
Selected primary task 'cross_project_task_2' from project :
Tasks to be executed: [task ':sub-project01:cross_project_task', task ':cross_project_task_2']
Tasks that were excluded: []
Resolve mutations for :sub-project01:cross_project_task (Thread[Execution worker,5,main]) started.
:sub-project01:cross_project_task (Thread[Execution worker,5,main]) started.
> Task :sub-project01:cross_project_task
Caching disabled for task ':sub-project01:cross_project_task' because:
Build cache is disabled
Task ':sub-project01:cross_project_task' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is task in sub-project01
Resolve mutations for :cross_project_task_2 (Thread[Execution worker,5,main]) started.
:cross_project_task_2 (Thread[Execution worker,5,main]) started.
> Task :cross_project_task_2
Caching disabled for task ':cross_project_task_2' because:
Build cache is disabled
Task ':cross_project_task_2' is not up-to-date because:
Task has not declared any outputs despite executing actions.
This is task in gradle-root
BUILD SUCCESSFUL in 861ms
2 actionable tasks: 2 executed
Watched directory hierarchies: []
Tips:
1. If one task depends on multiple tasks, the execution order of dependent tasks is random. E.g. A depends on B and C, B may be executed first, and C may be executed first as well.
2. Repeatedly dependent tasks will only be executed once. E.g. A depends on B and C, D also depends on C. So C can only be executed once.
11.3 Task Execution
gradle dependencies
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle dependencies
> Configure project :
This is a simple task.
> Task :dependencies
------------------------------------------------------------
Root project 'gradle-root'
------------------------------------------------------------
annotationProcessor - Annotation processors and their dependencies for source set 'main'.
No dependencies
compileClasspath - Compile classpath for source set 'main'.
No dependencies
compileOnly - Compile-only dependencies for the 'main' feature. (n)
No dependencies
default - Configuration for default artifacts. (n)
No dependencies
implementation - Implementation dependencies for the 'main' feature. (n)
No dependencies
mainSourceElements - List of source directories contained in the Main SourceSet. (n)
No dependencies
runtimeClasspath - Runtime classpath of source set 'main'.
No dependencies
runtimeElements - Runtime elements for the 'main' feature. (n)
No dependencies
runtimeOnly - Runtime-only dependencies for the 'main' feature. (n)
No dependencies
testAnnotationProcessor - Annotation processors and their dependencies for source set 'test'.
No dependencies
testCompileClasspath - Compile classpath for source set 'test'.
+--- org.junit:junit-bom:5.10.0
| +--- org.junit.jupiter:junit-jupiter:5.10.0 (c)
| +--- org.junit.jupiter:junit-jupiter-api:5.10.0 (c)
| +--- org.junit.jupiter:junit-jupiter-params:5.10.0 (c)
| \--- org.junit.platform:junit-platform-commons:1.10.0 (c)
\--- org.junit.jupiter:junit-jupiter -> 5.10.0
+--- org.junit:junit-bom:5.10.0 (*)
+--- org.junit.jupiter:junit-jupiter-api:5.10.0
| +--- org.junit:junit-bom:5.10.0 (*)
| +--- org.opentest4j:opentest4j:1.3.0
| +--- org.junit.platform:junit-platform-commons:1.10.0
| | +--- org.junit:junit-bom:5.10.0 (*)
| | \--- org.apiguardian:apiguardian-api:1.1.2
| \--- org.apiguardian:apiguardian-api:1.1.2
\--- org.junit.jupiter:junit-jupiter-params:5.10.0
+--- org.junit:junit-bom:5.10.0 (*)
+--- org.junit.jupiter:junit-jupiter-api:5.10.0 (*)
\--- org.apiguardian:apiguardian-api:1.1.2
testCompileOnly - Compile only dependencies for source set 'test'. (n)
No dependencies
testImplementation - Implementation only dependencies for source set 'test'. (n)
+--- org.junit:junit-bom:5.10.0 (n)
\--- org.junit.jupiter:junit-jupiter (n)
testRuntimeClasspath - Runtime classpath of source set 'test'.
+--- org.junit:junit-bom:5.10.0
| +--- org.junit.jupiter:junit-jupiter:5.10.0 (c)
| +--- org.junit.jupiter:junit-jupiter-api:5.10.0 (c)
| +--- org.junit.jupiter:junit-jupiter-params:5.10.0 (c)
| +--- org.junit.jupiter:junit-jupiter-engine:5.10.0 (c)
| +--- org.junit.platform:junit-platform-commons:1.10.0 (c)
| \--- org.junit.platform:junit-platform-engine:1.10.0 (c)
\--- org.junit.jupiter:junit-jupiter -> 5.10.0
+--- org.junit:junit-bom:5.10.0 (*)
+--- org.junit.jupiter:junit-jupiter-api:5.10.0
| +--- org.junit:junit-bom:5.10.0 (*)
| +--- org.opentest4j:opentest4j:1.3.0
| \--- org.junit.platform:junit-platform-commons:1.10.0
| \--- org.junit:junit-bom:5.10.0 (*)
+--- org.junit.jupiter:junit-jupiter-params:5.10.0
| +--- org.junit:junit-bom:5.10.0 (*)
| \--- org.junit.jupiter:junit-jupiter-api:5.10.0 (*)
\--- org.junit.jupiter:junit-jupiter-engine:5.10.0
+--- org.junit:junit-bom:5.10.0 (*)
+--- org.junit.platform:junit-platform-engine:1.10.0
| +--- org.junit:junit-bom:5.10.0 (*)
| +--- org.opentest4j:opentest4j:1.3.0
| \--- org.junit.platform:junit-platform-commons:1.10.0 (*)
\--- org.junit.jupiter:junit-jupiter-api:5.10.0 (*)
testRuntimeOnly - Runtime only dependencies for source set 'test'. (n)
No dependencies
(c) - A dependency constraint, not a dependency. The dependency affected by the constraint occurs elsewhere in the tree.
(*) - Indicates repeated occurrences of a transitive dependency subtree. Gradle expands transitive dependen
cy subtrees only once per project; repeat occurrences only display the root of the subtree, followed by this annotation.
(n) - A dependency or dependency configuration that cannot be resolved.
A web-based, searchable dependency report is available by adding the --scan option.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.6/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 884ms
1 actionable task: 1 executed
gradle help --task [Task Name]
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle help --task D
> Configure project :
This is a simple task.
> Task :help
Detailed task information for D
Path
:D
Type
Task (org.gradle.api.Task)
Options
--rerun Causes the task to be re-run even if up-to-date.
Description
-
Group
Self Definition Task
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.6/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 853ms
1 actionable task: 1 executed
plugins{
id ‘java’
id ‘application’
}
mainClassName=’com.artists.Main’ // Specify main startup class path
gradle tasks
If you didn’t specify a group, you won’t see any printed out of your self-definition task in any group in the console. You can use IDEA → Gradle Side bar → Other to see your tasks.
If you want to specify a group, you can use the following way.
// Task Group
task D {
group "Self Definition Task"
doLast {
println "This is taskD"
}
}
You can see your task under Self Definition Task group right now.
gradle tasks --all
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle tasks --all
> Configure project :
This is a simple task.
> Task :tasks
------------------------------------------------------------
Tasks runnable from root project 'gradle-root'
------------------------------------------------------------
Application tasks
-----------------
run - Runs this project as a JVM application
Build tasks
-----------
assemble - Assembles the outputs of this project.
sub-project01:assemble - Assembles the outputs of this project.
sub-project02:assemble - Assembles the outputs of this project.
sub-project03:assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
sub-project01:build - Assembles and tests this project.
sub-project02:build - Assembles and tests this project.
sub-project03:build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
sub-project01:buildDependents - Assembles and tests this project and all projects that depend on it.
sub-project02:buildDependents - Assembles and tests this project and all projects that depend on it.
sub-project03: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.
sub-project01:buildNeeded - Assembles and tests this project and all projects it depends on.
sub-project02:buildNeeded - Assembles and tests this project and all projects it depends on.
sub-project03:buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
sub-project01:classes - Assembles main classes.
sub-project02:classes - Assembles main classes.
sub-project03:classes - Assembles main classes.
clean - Deletes the build directory.
sub-project01:clean - Deletes the build directory.
sub-project02:clean - Deletes the build directory.
sub-project03:clean - Deletes the build directory.
jar - Assembles a jar archive containing the classes of the 'main' feature.
sub-project01:jar - Assembles a jar archive containing the classes of the 'main' feature.
sub-project02:jar - Assembles a jar archive containing the classes of the 'main' feature.
sub-project03:jar - Assembles a jar archive containing the classes of the 'main' feature.
testClasses - Assembles test classes.
sub-project01:testClasses - Assembles test classes.
sub-project02:testClasses - Assembles test classes.
sub-project03:testClasses - Assembles test classes.
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the 'main' feature.
sub-project01:javadoc - Generates Javadoc API documentation for the 'main' feature.
sub-project02:javadoc - Generates Javadoc API documentation for the 'main' feature.
sub-project03:javadoc - Generates Javadoc API documentation for the 'main' feature.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle-root'.
sub-project01:buildEnvironment - Displays all buildscript dependencies declared in project ':sub-project01'.
sub-project02:buildEnvironment - Displays all buildscript dependencies declared in project ':sub-project02'.
sub-project03:buildEnvironment - Displays all buildscript dependencies declared in project ':sub-project03'.
dependencies - Displays all dependencies declared in root project 'gradle-root'.
sub-project01:dependencies - Displays all dependencies declared in project ':sub-project01'.
sub-project02:dependencies - Displays all dependencies declared in project ':sub-project02'.
sub-project03:dependencies - Displays all dependencies declared in project ':sub-project03'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle-root'.
sub-project01:dependencyInsight - Displays the insight into a specific dependency in project ':sub-project01'.
sub-project02:dependencyInsight - Displays the insight into a specific dependency in project ':sub-project02'.
sub-project03:dependencyInsight - Displays the insight into a specific dependency in project ':sub-project03'.
help - Displays a help message.
sub-project01:help - Displays a help message.
sub-project02:help - Displays a help message.
sub-project03:help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
sub-project01:javaToolchains - Displays the detected java toolchains.
sub-project02:javaToolchains - Displays the detected java toolchains.
sub-project03:javaToolchains - Displays the detected java toolchains.
outgoingVariants - Displays the outgoing variants of root project 'gradle-root'.
sub-project01:outgoingVariants - Displays the outgoing variants of project ':sub-project01'.
sub-project02:outgoingVariants - Displays the outgoing variants of project ':sub-project02'.
sub-project03:outgoingVariants - Displays the outgoing variants of project ':sub-project03'.
projects - Displays the sub-projects of root project 'gradle-root'.
sub-project01:projects - Displays the sub-projects of project ':sub-project01'.
sub-project02:projects - Displays the sub-projects of project ':sub-project02'.
sub-project03:projects - Displays the sub-projects of project ':sub-project03'.
properties - Displays the properties of root project 'gradle-root'.
sub-project01:properties - Displays the properties of project ':sub-project01'.
sub-project02:properties - Displays the properties of project ':sub-project02'.
sub-project03:properties - Displays the properties of project ':sub-project03'.
resolvableConfigurations - Displays the configurations that can be resolved in root project 'gradle-root'.
sub-project01:resolvableConfigurations - Displays the configurations that can be resolved in project ':sub-project01'.
sub-project02:resolvableConfigurations - Displays the configurations that can be resolved in project ':sub-project02'.
sub-project03:resolvableConfigurations - Displays the configurations that can be resolved in project ':sub-project03'.
tasks - Displays the tasks runnable from root project 'gradle-root' (some of the displayed tasks may belong to subprojects).
sub-project01:tasks - Displays the tasks runnable from project ':sub-project01'.
sub-project02:tasks - Displays the tasks runnable from project ':sub-project02'.
sub-project03:tasks - Displays the tasks runnable from project ':sub-project03'.
Self Definition Task tasks
--------------------------
D
Verification tasks
------------------
check - Runs all checks.
sub-project01:check - Runs all checks.
sub-project02:check - Runs all checks.
sub-project03:check - Runs all checks.
test - Runs the test suite.
sub-project01:test - Runs the test suite.
sub-project02:test - Runs the test suite.
sub-project03:test - Runs the test suite.
Other tasks
-----------
A
B
C
C_2
C_3
compileJava - Compiles main Java source.
sub-project01:compileJava - Compiles main Java source.
sub-project02:compileJava - Compiles main Java source.
sub-project03:compileJava - Compiles main Java source.
compileTestJava - Compiles test Java source.
sub-project01:compileTestJava - Compiles test Java source.
sub-project02:compileTestJava - Compiles test Java source.
sub-project03:compileTestJava - Compiles test Java source.
components - Displays the components produced by root project 'gradle-root'. [deprecated]
sub-project01:components - Displays the components produced by project ':sub-project01'. [deprecated]
sub-project02:components - Displays the components produced by project ':sub-project02'. [deprecated]
sub-project03:components - Displays the components produced by project ':sub-project03'. [deprecated]
sub-project01:cross_project_task
cross_project_task_2
dependentComponents - Displays the dependent components of components in root project 'gradle-root'. [deprecated]
sub-project01:dependentComponents - Displays the dependent components of components in project ':sub-project01'. [deprecated]
sub-project02:dependentComponents - Displays the dependent components of components in project ':sub-project02'. [deprecated]
sub-project03:dependentComponents - Displays the dependent components of components in project ':sub-project03'. [deprecated]
model - Displays the configuration model of root project 'gradle-root'. [deprecated]
sub-project01:model - Displays the configuration model of project ':sub-project01'. [deprecated]
sub-project02:model - Displays the configuration model of project ':sub-project02'. [deprecated]
sub-project03:model - Displays the configuration model of project ':sub-project03'. [deprecated]
prepareKotlinBuildScriptModel
processResources - Processes main resources.
sub-project01:processResources - Processes main resources.
sub-project02:processResources - Processes main resources.
sub-project03:processResources - Processes main resources.
processTestResources - Processes test resources.
sub-project01:processTestResources - Processes test resources.
sub-project02:processTestResources - Processes test resources.
sub-project03:processTestResources - Processes test resources.
startScripts - Creates OS specific scripts to run the project as a JVM application.
task_1
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.6/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 922ms
1 actionable task: 1 executed
gradle tasks --group="[Group Name]"
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle tasks --group="Self Definition Task"
> Configure project :
This is a simple task.
> Task :tasks
------------------------------------------------------------
Tasks runnable from root project 'gradle-root'
------------------------------------------------------------
Self Definition Task tasks
--------------------------
D
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.6/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 877ms
1 actionable task: 1 executed
-h, --help
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle --help
To see help contextual to the project, use gradle help
USAGE: gradle [option...] [task...]
-?, -h, --help Shows this help message.
-a, --no-rebuild Do not rebuild project dependencies.
-b, --build-file Specify the build file. [deprecated]
--build-cache Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds.
--no-build-cache Disables the Gradle build cache.
-c, --settings-file Specify the settings file. [deprecated]
--configuration-cache Enables the configuration cache. Gradle will try to reuse the build configuration from previous builds.
--no-configuration-cache Disables the configuration cache.
--configuration-cache-problems Configures how the configuration cache handles problems (fail or warn). Defaults to fail.
--configure-on-demand Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating]
--no-configure-on-demand Disables the use of configuration on demand. [incubating]
--console Specifies which type of console output to generate. Values are 'plain', 'auto' (default), 'rich' or 'verbose'.
--continue Continue task execution after a task failure.
--no-continue Stop task execution after a task failure.
-D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue).
-d, --debug Log in debug mode (includes normal stacktrace).
--daemon Uses the Gradle daemon to run the build. Starts the daemon if not running.
--no-daemon Do not use the Gradle daemon to run the build. Useful occasionally if you have configured Gradle to always run with the daemon by default.
--export-keys Exports the public keys used for dependency verification.
-F, --dependency-verification Configures the dependency verification mode. Values are 'strict', 'lenient' or 'off'.
--foreground Starts the Gradle daemon in the foreground.
-g, --gradle-user-home Specifies the Gradle user home directory. Defaults to ~/.gradle
-I, --init-script Specify an initialization script.
-i, --info Set log level to info.
--include-build Include the specified build in the composite.
-M, --write-verification-metadata Generates checksums for dependencies used in the project (comma-separated list)
-m, --dry-run Run the builds with all task actions disabled.
--max-workers Configure the number of concurrent workers Gradle is allowed to use.
--offline Execute the build without accessing network resources.
-P, --project-prop Set project property for the build script (e.g. -Pmyprop=myvalue).
-p, --project-dir Specifies the start directory for Gradle. Defaults to current directory.
--parallel Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use.
--no-parallel Disables parallel execution to build projects.
--priority Specifies the scheduling priority for the Gradle daemon and all processes launched by it. Values are 'normal' (default) or 'low'
--profile Profile build execution time and generates a report in the <build_dir>/reports/profile directory.
--project-cache-dir Specify the project-specific cache directory. Defaults to .gradle in the root project directory.
-q, --quiet Log errors only.
--refresh-keys Refresh the public keys used for dependency verification.
--rerun-tasks Ignore previously cached task results.
-S, --full-stacktrace Print out the full (very verbose) stacktrace for all exceptions.
-s, --stacktrace Print out the stacktrace for all exceptions.
--scan Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (https://gradle.com/build-scans)
--no-scan Disables the creation of a build scan. For more information about build scans, please visit https://gradle.com/build-scans.
--status Shows status of running and recently stopped Gradle daemon(s).
--stop Stops the Gradle daemon if it is running.
-t, --continuous Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change.
-U, --refresh-dependencies Refresh the state of dependencies.
--update-locks Perform a partial update of the dependency lock, letting passed in module notations change version. [incubating]
-V, --show-version Print version info and continue.
-v, --version Print version info and exit.
-w, --warn Set log level to warn.
--warning-mode Specifies which mode of warnings to generate. Values are 'all', 'fail', 'summary'(default) or 'none'
--watch-fs Enables watching the file system for changes, allowing data about the file system to be re-used for the next build.
--no-watch-fs Disables watching the file system.
--write-locks Persists dependency resolution for locked configurations, ignoring existing locking information if it exists
-x, --exclude-task Specify a task to be excluded from execution.
-- Signals the end of built-in options. Gradle parses subsequent parameters as only tasks or task options.
-v, --version
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle -v
------------------------------------------------------------
Gradle 8.6
------------------------------------------------------------
Build time: 2024-02-02 16:47:16 UTC
Revision: d55c486870a0dc6f6278f53d21381396d0741c6e
Kotlin: 1.9.20
Groovy: 3.0.17
Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM: 17.0.10 (Oracle Corporation 17.0.10+11-LTS-240)
OS: Windows 11 10.0 amd64
--rerun-tasks
Gradle automatically handles up-to-date checks for output files and directories, but what if the task output is something else entirely? Perhaps it’s an update to a web service or a database table. Or sometimes you have a task which should always run.
That’s where the doNotTrackState() method on Task comes in. One can use this to disable up-to-date checks completely for a task, like so:
tasks.register('alwaysInstrumentClasses', Instrument) {
classFiles.from layout.files(tasks.named('compileJava'))
destinationDir = file(layout.buildDirectory.dir('instrumented'))
doNotTrackState("Instrumentation needs to re-run every time")
}
tasks.create("E"){
doLast {
println("This is taskE")
}
}
// register() is delayed creation, tasks will only be created
// when needed
tasks.register("f"){
doLast{
println("This is taskF")
}
}
task D {
group "Self Definition Task"
doLast {
println "This is taskD"
}
}
task(group:"artists", description:"This is internal definition of task properties","G")
task "H"{
group("artists")
description("This is another way to set task properties of task H")
}
task "I"{}
I.group="artists"
// Task D belongs to Self Definition Task, we will move it to artists
D.group("artists")
11.5 Task Type
Previously we all used DefaultTask. If we want to complete some specific operations, it would be a little complex. In Gradle, we can specify the task type and then use the corresponding API directly.
Copies files into a destination directory. This task can also rename and filter files as it copies. The task implements CopySpec for specifying what to copy.
Generates an HTML dependency report. This report combines the features of the ASCII dependency report and those of the ASCII dependency insight report. For a given project, it generates a tree of the dependencies of every configuration, and each dependency can be clicked to show the insight of this dependency.
Copies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.
/**
* Dynamically assign tasks
* After you define a task, you can use it directly
*/
4.times { counter ->
tasks.register("task${counter}"){
doLast {
println "This is task ${counter}"
}
}
}
tasks.named('task0'){dependsOn('task2','task3')}
The log is here
PS C:\GitRepository\Daydayup\Gradle\gradle-root> gradle task0
> Configure project :
This is a simple task.
> Task :task2
This is task 2
> Task :task3
This is task 3
> Task :task0
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.6/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 executed
11.8 Closing and opening tasks
/**
* Closing and opening tasks
* 1. Internal set enable(false), false means the task will be skipped.
* 2. Outer set [task name].enabled = false, false means the task will be skipped.
*/
task disableTask{
doLast{
println "This task is disable task"
}
enabled(false) // The default is true
}
//disableTask.enabled=false
11.9 Timeout
Each task will have a timeout to restrict the execution time, the task overtime will be killed. But if you use --continue , the successor tasks will be executed as normal, otherwise it will failed. When we run gradle timeoutTask_1 timeoutTask_2 , it will show build failed. You can also try gradle timeoutTask_1 timeoutTask_2 --continue , it will also show build failed. but the timeoutTask_2 will be executed.
By name. This only supports finding tasks in the current project.
By path. This can find tasks in specific modules or projects.
task findTask{
doLast {
println "This is a task about finding task."
}
}
tasks.findByName("findTask").doFirst {println "Add doFirst() before findTask byName"}
tasks.getByName("findTask").doFirst {println "Add doFirst() before findTask getName"}
tasks.findByPath(":findTask").doFirst {println "Add doFirst() before findTask byPath"}
tasks.getByPath(":findTask").doFirst {println "Add doFirst() before findTask getPath"}
11.11 Task does not exist
When we execute a task which doesn’t exist, it will report an error. How can we print a hint rather than report an error?
/**
* Task doesn't exist
* If you run gradle abc taskExist -> report an error in normal
* For now it will print the hint, addRule([Description], Closure)
* The given rule is invoked when an unknown object is requested by name.
* The abc is the unknown object.
*/
task "taskExist"{
doLast {
println "This is taskExist."
}
}
tasks.addRule("[Description of rule]",{
def taskName -> task(taskName){
println "The unknow object is ${taskName}"
}
})
Why this rule only take effect on abc rather than taskExist. The official document said “Adds a rule to this collection. The given rule is invoked when an unknown object is requested by name.”
11.12 OnlyIf
How to let a task to be executed under a specific condition? Such as the keyword if in Java. There is a onlyIf, abstract fun onlyIf(onlyIfClosure: Closure) In business, when you want to control a JUnit test, you can use this.
/**
* onlyIf
* Before you test, please ensure the line 231-235 comment out
* first. Why? We can only use 248, and 249 to add a property
* to the project. Task is a property of the project object as
* well.
* gradle onlyIfTask -> will run
* gradle propertyExist_3 -> will run
*/
task "onlyIfTask" {
doLast {
println "This is onlyIfTask."
}
}
// You can use the following way to add a property to the project object
// You can also use gradle onlyIfTask -PpropertyExist=true to add a property to
// the project object
ext.propertyExist=true
ext {
propertyExist_2=true
}
onlyIfTask.onlyIf {project.hasProperty("propertyExist")}
task "propertyExist_3" { println "100% task is a property of the project object"}
onlyIfTask.onlyIf {project.hasProperty("propertyExist_3")}
11.13 Default Task
No tasks names are provided when starting the build, we want it to run a default task. We can use default tasks. Attention, this will be executed when no tasks names provided. What is this means? gradle -q or gradle
File collection is a set of file. We can use Project.files(java.lang.Object[]) to create a FileCollection instance.
/**
* File Collection
*/
FileCollection fileCollection = files('src/fileCollection_1.xml',['src/fileCollection_2.xml','src/fileCollection_3.xml'])
fileCollection.forEach {
item -> {
println(item.name)
item.createNewFile()
}
}
// Convert fileCollection to Set
Set filesSet = fileCollection.files
for (def i in filesSet){
println "fileSet " + i.exists()
}
Set fileSet_2 = fileCollection as Set
for (def i in fileSet_2){
println "fileSet_2 " +i.exists()
}
// Convert fileCollection to List
Set fileList = fileCollection as List
// Add or remove file of fileCollection
def addFile = fileCollection + files('src/fileCollection_4.xml')
def removeFile = fileCollection - files('src/fileCollection_4.xml')
addFile.forEach { File item -> {
println "AddFile " + item.name
}}
12.3 File Tree
A file tree is a hierarchical collection of files.
/**
* File Tree
* 1. Use path to create file tree object
* 2. Use closure to create file tree object
* 3. Use path and closure to create file tree
*/
ConfigurableFileTree configurableFileTree = fileTree('src/main')
configurableFileTree.forEach (item->{
println "File tree - ${item.name}"
})
// Exclude Main.java -> It won't print anything
configurableFileTree.exclude("**/*.java").forEach (item->{
println "File tree exclude - ${item.name}"
})
// 2. Use closure to create file tree
def tree = fileTree('src/main').include("**/*.java")
tree.forEach (item -> {println "Use closure to create file tree - ${item.name}"})
// 3. Use path and closure to create file tree
tree = fileTree(dir: 'src/main',include:'**/*.java')
tree = fileTree(dir: 'src/main',includes:['**/*.java','**/*.xml'],excludes: '**/*Txt*/**')
12.4 File Copy
File copy can use Copy Task to copy file, filter file, and rename file. (If you use IDEA, after you run a task, please reload the task and then you can see the files. Or you can go to the target directory in the computer to check.)
/**
* File Copy
* from([file path])
* into([directory path])
* 1. If file path is a directory -> copy all files in this directory
* 2. If file path is a file -> copy this file
* 3. If file path is a zip -> copy file in this zip
*/
task copyTasks(type:Copy){
// Copy files in the folder. If the folder doesn't exist, it will be ignored.
from 'src/main/webapp'
// Copy a specific file
from 'src/main/java/com/artists/TestCopyFile.csv'
// Copy from zip
from zipTree('src/main/TestZip.zip')
// Copy files to target directory
into 'build/copyFile'
//into this.buildDir.absolutePath
}
// only copy .csv file
task copyTasks_2(type:Copy){
from 'src/main/java/com/artists/TestCopyFile.csv'
from zipTree('src/main/TestZip.zip')
include('**/*.csv')
// exclude('**/*.csv')
into 'build/copyFile_2'
}
// rename the file
task copyTasks_3(type:Copy){
from 'src/main/java/com/artists/TestCopyFile.csv'
rename { def fileName ->
fileName.replace('CopyFile','CopyFile_3')
}
into 'build/copyFile_3'
}
// copy()
task copyTasks_4 {
copy {
from 'src/main/java/com/artists/TestCopyFile.csv'
into 'build/copyFile_4'
}
}
// define input & output outside
task copyTasks_5 {
inputs.file 'src/main/java/com/artists/TestCopyFile.csv'
outputs.dir 'build/copyFile_5'
doLast{
copy {
from 'src/main/java/com/artists/TestCopyFile.csv'
into 'build/copyFile_5'
}
}
}
12.5 Archive File
Sometime we need to create a zip, jar, war in a task, we can use the following way to do this thing.
/**
* Archive File
* archiveTask(type:Zip) -> type is the target type you want to use
* Target Zip name = [archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]
* Target file path is
*/
def archivesDirPath = layout.buildDirectory.dir('archiveFile')
task archiveTask(type:Zip) {
from 'src/main/java/com/artists/TestCopyFile.csv'
into 'build'
archiveBaseName ='archiveTask'
destinationDirectory= archivesDirPath
}
13. Dependencies
When you execute gradle build , gradle will download the corresponding jar from dependencies repository.
There are 3 kind of dependencies:
Direct Dependency
Project Dependency
Local Jar Dependency
/**
* There are 3 kind of dependencies:
* 1. Direct Dependency -> [Dependency type][Dependency group] [Dependency name][Dependency version]
* 2. Project Dependency
* 3. Local Jar Dependency -> can use file collection & file tree
*/
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
// 1. Direct Dependency
implementation group: 'log4j', name: 'log4j', version: '1.2.17'
implementation 'log4j:log4j:1.2.17'
// 2. Project Dependency, you must define this module in settings.gradle
implementation project(':sub-project01')
// 3. Local Jar Dependency
// Dependent on mysql.jar under lib, lib and src are the same level directories
implementation files('lib/mysql.jar','lib/log4j.jar')
// Dependent on all jars under lib directory, can also use excludes
implementation fileTree('dir':'lib',includes: ['*.jar'])
}
Dependencies Type
This is same with the <scope> in maven.
Dependencies Type
Description
compileOnly
Only use in compile stage but won’t package it. (Provided by Java plugin)
runtimeOnly
Only use in runtime stage but won’t use in compile stage. E.g. mysql driver (Provided by Java plugin)
implementation
Active in compile and runtime stage. (Provided by Java plugin)
testCompileOnly
Only use in compile test stage but won’t use in runtime stage. (Provided by Java plugin)
testRuntimeOnly
Only use in runtime test stage but won’t use in compile stage. (Provided by Java plugin)
testImplementation
Only for test in [src/test], (Provided by Java Plugin)
providedCompile
It is used in the compile and test stages, and the container in the runtime stage already provides corresponding support; there is no need to package it into war. E.g. servlet-api.jar (Provided by war plugin)
compile
Compile all dependencies and use in all classpath, and will be packaged. Remove after v7.0
runtime
Only use in compile and test stage but not in compile stage. E.g. mysql driver. Remove after v7.0
compileOnlyApi
Use in declaring a module and compiling, but not in runtime stage. (Provided by java-library)
Difference between api and implementation
ㅤ
compileOnlyApi
implementation
Compile
Dependency Transmission. If the underlying dependencies change, then the upper-level dependencies must also change. So the compilation speed is slower.
Dependency Transmission. So the compilation speed is faster.
Runtime
All class of modules will be loaded.
All class of modules will be loaded.
Application Scenario
Applicable to multi-module dependencies to avoid duplicate dependencies. (Only multiple modules, we need to consider to usecompileOnlyApi)
In most cases use implementation
Example 1:
If dependency_B changes, dependency_B, dependency_A, and the project will be re-compiled.
If dependency_D changes, only dependency_D and dependency_C will be re-compiled.
Example 2:
A implementation B, B implementation C, so A can’t use C.
A implementation B, B API C, so A can use C.
A implementation B, B implementation C, C API D, so B can use D, but A can’t.
A implementation B, B API C, C API D, so A can use D.
Dependencies Conflict
Example:
Root project A has 2 sub modules, B and C. B depend on log4j1.4.2, but C depend on log4j 2.2.4 The question is which version project A will use?
Answer:
Gradle will use the latest version, for example, the latest version log4j is 3.1, so Gradle will use 3.1 rather than 1.4.2 or 2.2.4. Why Gradle will use latest version? Because most jar backward compatibility. This is a default way, you don’t need to do anything
Method 2:
Gradle also provide the exclude like Maven
// Exclude dependencies
implementation('org.hibernate:hibernate-core:3.6.3.Final'){
exclude group:'org.slf4j'
// You can also use module to exclude the dependency
exclude module:'slf4j-api'
exclude group:'org.slf4j',module:'slf4j-api'
}
Method 3: (Better not use this way)
Preventing dependency propagation. When we import a dependency, it will show like the following screenshot.
If we prevent dependency propagation, it will looks like
Method 4: (The official documentation recommends using this method)
Force a specific version
implementation 'org.slf4j:slf4j-api:1.4.0!!'
// + means to find the latest version in repositories{}.
// The following 2 ways called dynamic version declaration
implementation 'org.slf4j:slf4j-api:+'
implementation 'org.slf4j:slf4j-api:latest.integration'
// Force a specific version
implementation 'org.slf4j:slf4j-api:1.4.0'{
version{
strictly("1.4.0")
}
}
How do we check which dependency conflicts?
// When there is dependencies conflict, it will failed during the build stage
configurations.all(){
Configuration configuration -> {
configuration.resolutionStrategy.failOnVersionConflict()
}
}
14. Gradle Plugins
Q1: Why we use plugins?
Promote code reuse, improve code efficiency.
Promote higher degree of modularization, automation and convenience of projects.
Pluggable expansion project functions.
Q2: What are plugins for?
Add task into project to help complete test, compile, package.
Can add scope for dependencies.
Can extend new properties, methods, and so on.
Some agreements can be made on the project. E.g. we add java plugin, the convention is that main/java/src is the storage location of the source code.
Script Plugin
It is essentially a script, such as I define a script_plugin.gradle
// This is for script_plugin test
ext {
author = "ArtistS"
conf=[
jdkVersion: JavaVersion.VERSION_1_8
]
spring=[
version:'5.0.0'
]
}
And in the build.gradle you can use keyword apply from to import the script.
// In the build.gradle file
/**
* Script Plugin
* apply from: [file absolute path | relative path]
*/
apply from:'script_plugin.gradle'
task scriptPluginTest{
doLast {
println("The author is ${author}, and JDK version is ${conf.jdkVersion}, the spring version is ${spring.version}")
}
}
Where we can use script plugin?
In my opinion, we can use it to manage the whole version we use in the project, we only need to change one version in one file. And we can also use script plugin to manage the application version. In our project, we will have multiple modules, we can use this way to manage the module version in one place.
Binary Plugin(Object Plugin)
A plugin implements org.gradle.Plugin interface called Binary Plugin, each Java Gradle Plugin will have a plugin id (This id is unique).
Internal Plugin (Core Plugin)
If you want to check how many core plugin in the Gradle, you can open the following link .
// In build.gradle
// 1. Use plugins DSL
plugins{
id 'java'
}
// 2. apply(map specific parameters)
// apply[plugin id || Full Class Name || Plugin Class Name]
apply plugin:'java' // plugin id
apply plugin:org.gradle.api.plugins.JavaPlugin // Full Class Name
apply plugin:JavaPlugin // Plugin Class Name
// 3. apply(closure)
apply {
plugin 'java'
}
Third Party Plugin
//Traditional usage -> buidscript tag must be the first tag in build.gradle file
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
// import plugins here first
dependencies {
classpath("uk.gov.hmcts.reform:ccd-gradle-plugin:5.5.6")
}
}
// Then apply plugin
apply(plugin = "hmcts.ccd.sdk")// You already import first, no need to write version
// Plugin DSL
// If the third-party plugin be hosted on the following website, you don't need
// to write classpath in the buildscript.https://plugins.gradle.org/
plugins {
id("hmcts.ccd.sdk") version "5.5.6"
}
Custom Plugin
Conclusion: In most cases, we won’t use custom plugin. These plugins can only be used in the current project, and the other projects can’t use.
package com.artists
import org.gradle.api.Plugin
import org.gradle.api.Project
class BuildSrcPlugin implements Plugin<Project>{
@Override
void apply(Project project) {
// This name can't include _ e.g. Custom_Plugin
project.task("globalPlugin"){
doLast{
println("This is global custom plugin!")
}
}
}
}
4. Create resources/META-INF/gradle-plugins/properties under src. And create com.artists.plugin.properties.(This file must be [Package Name of Test.groovy].properties, and [Package Name of Test.groovy] is the id of this plugin)
And update the com.artists.plugin.properties
# implementation-class=[full class name of the custom plugin class]
implementation-class=com.artists.Test
Import the plugin in the module you want to use. E.g. I will add this into the gradle-root
apply plugin: 'BuildSrcPlugin'
// Or you can also use this way, if you use
// this way you don't need to do step4
/**
* Apply Global Custom Plugin
*/
apply plugin: 'java-gradle-plugin'
gradlePlugin {
plugins {
greeting {
// plugin id
id = 'BuildSrcPlugin'
// implementation class
implementationClass = 'com.artists.BuildSrcPlugin'
}
}
}
Custom Plugin (Can use in different project)
If you want your custom plugins can use in any project, you should publish it to maven repository.
All configurations in gradle.properties are global and can be used directly in build.gradle of each module. Some data we don't want others to see, we can put into this file, and don't upload this file to EDH. This file can also change and unify some properties to improve# development efficiency.
# JVM heap size (If you want to run faster, you can increase this value)
Dorg.gradle.jvmargs=-Xmx5120m -XX:MaxPermSize=1280m -Dfile.encoding=UTF-8
# Open daemon,the next build will use this daemon as well rather than fork a new gradle process.
#Dorg.gradle.daemon=true
# Load on demand
#Dorg.gradle.configureondemand=true
# Parallel compile
#Dorg.gradle.parallel=true
# Open Gradle cache
#Dorg.gradle.caching=true
# Workers
# max-workers=8
N.1 build.gradle
// Specify the JDK version to compile source code(Can only use with java plugin)
// Related to compile env
sourceCompatibility = 1.8
// Generate specific JDK version's class file(Can only use with Java plugin)
// Related to running env
targetCompatibility = 1.8
// Compile character set encoding
compileJava.options.encoding "UTF-8"
// Test character set encoding
compileTestJava.options.encoding "UTF-8"
// Compile JAVA file with UTF-8 -> source code
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
// Compile JAVA file with UTF-8 -> doc
tasks.withType(Javadoc) {
options.encoding "UTF-8"
}
/**
* Repositories
* Gradle will try to find the dependencies by the order
*/
repositories {
maven { url 'file:///D:/repos/mavenrepos3.5.4' }
maven { url "$rootDir/lib/release" }
// Try to find dependencies in local Maven Repositories
mavenLocal()
// Try to find dependencies in third-party repositories and private repositories
maven { name "Alibaba"; url "https://maven.aliyun.com/repository/public" }
maven { name "Bestek"; url "https://nexus.bsdn.org/content/groups/public" }
// Try to find dependencies in remote Maven repositories
mavenCentral()
// Try to find dependencies in google repositories
google()
}
/**
* allprojects
* allprojects will configure root project + child project
* It will print
* gradle-root
* buildSrc
* gloal-custom-plugin
* sub-project01
* sub-project02
* sub-project03
*/
allprojects {
tasks.create('configureAllProject') {
println "project name is $project.name"
}
}
/**
* Sub-projects
* subprojects will configure all child projects
* It will print
* buildSrc
* gloal-custom-plugin
* sub-project01
* sub-project02
* sub-project03
*/
subprojects {
tasks.create('configureAllProject') {
println "project name is $project.name"
}
}
/**
* project
* If you configure repositories and dependencies in the root
* project, these dependencies can only be used in the root
* project. If you want to use in the child project, you should
* configure them by project.
* You can use project('[sub project name exist in settings.gradle]')
* to configure a specific project.
* E.g. Add a dependencies into the sub-project01
*/
project('sub-project01'){
apply plugin: 'java'
dependencies {
implementation 'log4j:log4j:1.2.17'
}
}