Extending Gradle with a Custom Enhanced Task: Difference between revisions
Line 26: | Line 26: | ||
An enhanced task requires writing Groovy or Java code either in [[Extending_Gradle#In-line_in_build.gradle|in-line in build.gradle]] or [[Extending_Gradle#In-line_in_a_script_plugin|a script plugin file]], or into a source code file (or files) maintained in [[Extending_Gradle#In_the_Project.27s_buildSrc_Directory|the project's buildSrc]] or [[Extending_Gradle#External_to_Project|externally]]. The behavior is built into the task and the task exposes some properties that can be configured from the build script. | An enhanced task requires writing Groovy or Java code either in [[Extending_Gradle#In-line_in_build.gradle|in-line in build.gradle]] or [[Extending_Gradle#In-line_in_a_script_plugin|a script plugin file]], or into a source code file (or files) maintained in [[Extending_Gradle#In_the_Project.27s_buildSrc_Directory|the project's buildSrc]] or [[Extending_Gradle#External_to_Project|externally]]. The behavior is built into the task and the task exposes some properties that can be configured from the build script. | ||
An in-line custom enhanced task | An in-line custom enhanced task can be declared as follows, either in [[build.gradle]] or in a [[Gradle_Plugin_Concepts#Script_Plugin|script plugin]]: | ||
<syntaxhighlight lang='groovy'> | <syntaxhighlight lang='groovy'> | ||
... | ... |
Revision as of 07:43, 24 September 2020
External
Internal
Overview
The simplest way of extending Gradle is write a custom task. The custom task can be declared in-line in the default build script build.gradle, as a simple task. The simple task can also be declared in-line in a separate build script, which is then included from the default build script. The code of the custom task can live in a separate source file, which in turn can be declared in a special area of the Gradle project, or can be shared with other projects as part of a library, developed in its own project. Such a task is referred to as a enhanced task.
Declared tasks can be listed with:
gradle tasks --all
Simple Task
A simple task is defined with an in-line task action closure in the build script or another script imported from the build script.
task customSimpleTask {
println 'this is a simple task'
}
Examples of Gradle projects defining custom simple task declared in-line in build.gradle and custom simple task declared in-line in a script plugin.
Enhanced Task
An enhanced task requires writing Groovy or Java code either in in-line in build.gradle or a script plugin file, or into a source code file (or files) maintained in the project's buildSrc or externally. The behavior is built into the task and the task exposes some properties that can be configured from the build script.
An in-line custom enhanced task can be declared as follows, either in build.gradle or in a script plugin:
...
class CustomEnhancedTask extends DefaultTask {
@TaskAction
public void impl() {
System.out.println("this is an in-line enhanced task")
}
}
task custEnhTask(type: CustomEnhancedTask)
...
Examples:
- Custom ehanced Java task declared in-line in build.gradle
- Custom enhanced task developed in buildSrc.
TODO/TODEPLETE
DEPLETE Gradle_Task_TODEPLETE#Explicit_Task_Declaration_.28Custom_Tasks.29