About
A build process execute a graph of tasks.
Task may just be:
- or provided by plugin
Example
tasks.register("Hello") {
println("Hello")
}
- execution with the wrapper of the project
gradlew Hello
Hello
Model
Tasks themselves consist of:
- Actions — pieces of work that do something, like copy files or compile source
- Inputs — values, files and directories that the actions use or operate on
- Outputs — files and directories that the actions modify or generate
A task should never call another task.
Tasks should define inputs and outputs to get the performance benefits of incremental build functionality.
Output
When declaring the outputs of a task, make sure that the directory for writing outputs is unique among all the tasks in your project.
Intermingling or overwriting output files produced by different tasks compromises up-to-date checking causing slower builds. In turn, these filesystem changes may prevent Gradle’s build cache from properly identifying and caching what would otherwise be cacheable tasks.
Management
Create
Task are provided by:
Built-in
Example for copy
task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
from "src"
into "dest"
}
Register
tasks.register("logInfo") {
logging.captureStandardOutput(LogLevel.INFO)
doFirst {
println("A task message which is logged at INFO level")
}
}
Default
task echo(description: 'Default task') << {
println 'Hello'
}
defaultTasks 'echo'
List
- In one project directory
gradlew tasks
# or
gradlew tasks --all
- multi-project call from the root
gradle :services:webservice:tasks
Property
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
- Lookup
val testTask = tasks.getByName<Test>("test");
testTask.failFast = true;
- Or declarative
tasks.test {
failFast = true;
}
Execute
gradle -q taskName
where:
- -q stands for quiet
Exclude
- Specify a task to be excluded from execution.
gradle [-x|--exclude-task]
- Specify a task to be excluded dynamically. See How to skip a task if another task is executed in Gradle?
Cache
Ignore previously cached task results.
gradle --rerun-tasks
Help
gradlew help --task <task>
Disable
Run the builds with all task actions disabled.
gradle [-m|--dry-run]
Dependency
https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies
tasks.register("hello") {
doLast {
println("Hello world!")
}
}
tasks.register("intro") {
dependsOn("hello")
doLast {
println("I'm Gradle")
}
}