Write a task
A task of your own is one call of tasks.register: a name, a group, a description and the action that does the work. This lesson writes two of them, one with no type and one that copies a file, and runs both.
tasks.register
settings.gradle
rootProject.name = 'shop'
build.gradle
plugins {
id 'base'
}
tasks.register('hello') {
group = 'shop'
description = 'Prints a greeting'
doLast {
println 'Hello from a registered task'
}
}
Four things happen here. register creates the task lazily: the closure runs only when Gradle needs the task object, so a task nobody asked for costs nothing. group and description put it in gradle tasks. doLast is the action. And the action is a closure, which makes the task an Action of its own rather than an instance of a class.
Run it
gradle -q hello --console=plain --offline --no-daemon
Hello from a registered task
With -q only the printout remains. The full log would show > Task :hello before the line and BUILD SUCCESSFUL after it.
A task with a type
NOTES.md
# Notes
A file for the copy task to move.
A task that copies files needs a type, because the inputs and outputs of a copy are the work: Copy knows how to compare a source folder with a destination folder, and a closure knows nothing. The type brings from and into with it, and the whole build script is now this:
build.gradle
plugins {
id 'base'
}
tasks.register('hello') {
group = 'shop'
description = 'Prints a greeting'
doLast {
println 'Hello from a registered task'
}
}
tasks.register('copyNotes', Copy) {
group = 'shop'
description = 'Copies NOTES.md into build/notes'
from layout.projectDirectory.file('NOTES.md')
into layout.buildDirectory.dir('notes')
}
layout.projectDirectory and layout.buildDirectory are providers, not strings: they read the folders when the task runs, and the configuration cache can record them. The old form, "$buildDir/docs", still works but ties the script to a value that is read too early.
gradle copyNotes --console=plain --offline --no-daemon
> Task :copyNotes
BUILD SUCCESSFUL in 12s
1 actionable task: 1 executed
ls build/notes
NOTES.md
The task in the list
gradle tasks --group shop --console=plain --offline --no-daemon
> Task :tasks
------------------------------------------------------------
Tasks runnable from root project 'shop'
------------------------------------------------------------
Shop tasks
----------
copyNotes - Copies NOTES.md into build/notes
hello - Prints a greeting
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL in 11s
1 actionable task: 1 executed
Two lines, because both tasks have a description. A task without one is invisible in gradle tasks, which is the usual reason a task of your own seems to be missing.
Related
- doFirst and doLast: several actions on one task and the order they run in.
- Inputs, outputs and UP-TO-DATE: what the Copy type of this lesson gets for free.
- A plugin you write: the same task in a plugin, reused by several projects.
Автор статьи: Arthur Isaev