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

时间: 2024-12-23 08:39:39

Gradle Goodness: Adding Tasks to a Predefined Group的相关文章

Gradle Goodness: Group Similar Tasks

In Gradle we can assign a task to a group. Gradle uses the group for example in the output of $ gradle -t to output all the tasks of the same group together. We only have to set the group property with a value and our task belongs to a group. In the

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. One of the ways to alter the build configuration is with initialization or init scripts. These are like other Gradle scripts but are executed before t

Gradle Goodness: Display Available Tasks

To see which tasks are available for our build we can run Gradle with the command-line option -t or --tasks. Gradle outputs the available tasks from our build script. By default only the tasks which are dependencies on other tasks are shown. To see a

Gradle Goodness: Task Output Annotations Create Directory Automatically

Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of Gradle is incremental build support. With incremental build support a task is only executed if it is really necessary. For example if a task generate

Gradle Goodness: Copy Files with Filtering

Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filtering capabilities. This means we can change the contents of the files that are copied before they reach their new destination. We use the filter() method

org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection

韩梦飞沙  韩亚飞  [email protected]  yue31313  han_meng_fei_sha 错误:org.gradle.api.internal.tasks.DefaultTaskInputs $ TaskInputUnionFileCollection org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection不能投可能导致这个意外错误的原因包括:分级的依赖缓存可能是损坏的(这有时是

Gradle Goodness: Continue Build Even with Failed Tasks

If we run a Gradle build and one of the tasks fails, the whole build stops immediately. So we have fast feedback of our build status. If we don't want to this and want Gradle to execute all tasks, even though some might have failed, we use the comman

Gradle Goodness: Add Incremental Build Support to Tasks

Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unless it is necessary. We can help Gradle and configure our task so it is ready for an incremental build. Suppose we have a task that generates a file. T

Gradle Goodness: Automatic Clean Tasks

Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This task is able to remove any output files or directories we have defined for our task. For example we can assign an output file or directory to our task with