首页 > 代码库 > Gradle Goodness: Adding Tasks to a Predefined Group

Gradle Goodness: Adding Tasks to a Predefined Group

In Gradle we can group related tasks using the group property of a task. We provide the name of our group and if we look at the output of the tasks task we can see our tasks grouped in section with the given name. In the next sample we create a new task publish and assign it the group name Publishing.

view sourceprint?
0.task publish(type: Copy) {
1.from "sources"
2.into "output"
3.}
4. 
5.configure(publish) {   
6.group = ‘Publishing‘
7.description = ‘Publish source code to output directory‘
8.}

If we execute tasks we get the following output:

$ gradle tasks
:tasks
 
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
 
Help tasks
----------
dependencies - Displays the dependencies of root project ‘taskGroup‘.
help - Displays a help message
projects - Displays the sub-projects of root project ‘taskGroup‘.
properties - Displays the properties of root project ‘taskGroup‘.
tasks - Displays the tasks runnable from root project ‘taskGroup‘ (some of the displayed tasks may belong to subprojects).
 
Publishing tasks
----------------
publish - Publish source code to output directory
 
To see all tasks and more detail, run with --all.
 
BUILD SUCCESSFUL
 
Total time: 2.327 secs

Suppose we apply the Java plugin to our project. We get a lot of new tasks, which are already in groups with names like Build and Documentation. If we want to add our own custom tasks to one of those groups we only have to use the correct name for the group property of our task. In the following build file we apply the Java plugin and use the Build group name as a group name for our task. The name is defined as a constant of the BasePlugin.

view sourceprint?
00.apply plugin: ‘java‘
01. 
02.task publish(type: Copy) {
03.from ‘sources‘
04.into ‘output‘
05.}
06. 
07.configure(publish) {   
08.group = BasePlugin.BUILD_GROUP // Or use ‘build‘
09.description = ‘Publish source code to output directory‘
10.}

When we run tasks again we can see our task is in the Build section together with the tasks added by the Java plugin:

$ gradle tasks
:tasks
 
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
 
Build tasks
-----------
assemble - Assembles all Jar, War, Zip, and Tar archives.
build - Assembles and tests this project.
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.
classes - Assembles the main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
publish - Publish source code to output directory
testClasses - Assembles the test classes.
 
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
 
Help tasks
----------
dependencies - Displays the dependencies of root project ‘taskGroup‘.
help - Displays a help message
projects - Displays the sub-projects of root project ‘taskGroup‘.
properties - Displays the properties of root project ‘taskGroup‘.
tasks - Displays the tasks runnable from root project ‘taskGroup‘ (some of the displayed tasks may belong to subprojects).
 
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
 
Rules
-----
Pattern: build<configurationname>: Assembles the artifacts of a configuration.
Pattern: upload<configurationname>: Assembles and uploads the artifacts belonging to a configuration.
Pattern: clean<taskname>: Cleans the output files of a task.
 
To see all tasks and more detail, run with --all.
 
BUILD SUCCESSFUL
 
Total time: 2.896 secs

Gradle Goodness: Adding Tasks to a Predefined Group