Gradle Goodness: Working with Live Task Collection

Gradle support the definition of so called live collections. These collections are mostly created based on criteria like with a filter() or matching() method. The collection content can change if the content of the source collection changes. For example the org.gradle.api.DomainObjectCollection interface has a matching method that returns a live collection. The list of tasks in a project implements this interface so we can have a live collection of tasks that match a certain criteria.

The nice thing about the live collection of tasks is that we can add tasks to the project after we have defined the task collection and they will be included in the collection. Normally if we would access the list of tasks in a project and use for example a findAll method than the returned list of tasks will not change. So if we add a new task to the project it will not be added to the list of task that apply to the condition of the findAll method.

Now if we use the matching() method on a list of tasks the result will be a live list of tasks. Even if we add tasks to the project after the definition of the list of tasks, they will be added to the collection.

Let‘s see the following Gradle build file that defines the tasks allCompile and allCopy. Each task has dependencies on other tasks. We use the dependsOn() method of a task to set those dependencies. The dependsOn() method accepts a collection of other tasks. For the allCompile task we don‘t use a live collection and for the allCopy task we use a live collection.

view sourceprint?

00.task allCompile << {

01.println ‘All is compiled.‘

02.}

03.allCompile.dependsOn project.tasks.findAll {

04.it.name.startsWith(‘compile‘)

05.}

06.

07.task allCopy << {

08.println ‘Everything is copied.‘

09.}

10.// Set dependencies with live collection of tasks.

11.allCopy.dependsOn project.tasks.matching {

12.it.name.startsWith(‘copy‘)

13.}

14.

15.// Add dependency tasks.

16.5.times {

17.// Dependencies for compileAll task.

18.task "compile${it}" << { println ‘Compile the code.‘ }

19.

20.// Dependencies for copyAll task.

21.task "copy${it}" << { println ‘Copy something.‘ }

22.}

If we run the build script we get the following output:

$ gradle allCompile allCopy

:allCompile

All is compiled.

:copy0

Copy something.

:copy1

Copy something.

:copy2

Copy something.

:copy3

Copy something.

:copy4

Copy something.

:allCopy

Everything is copied.

BUILD SUCCESSFUL

Total time: 2.232 secs

We notice the allCompile tasks doesn‘t have any dependencies, because those task dependencies didn‘t exist when we used the dependsOn() method. The allCopy task has all task dependencies even though we created them later in the build script.

Bonus: we can use the findAll method to look for task dependencies, but we have to let Gradle evaluate this condition in a closure. So we can change our build script and use a closure with the dependsOn() method. Gradle will invoke the closure at execution time and not a configuration time. The dependencies tasks are then available and assigned as dependencies to the allCompile task:

view sourceprint?

00.task allCompile << {

01.println ‘All is compiled.‘

02.}

03.// Use closure for resolving task dependencies.

04.allCompile.dependsOn {

05.project.tasks.findAll {

06.it.name.startsWith(‘compile‘)

07.}

08.}

09.

10.task allCopy << {

11.println ‘Everything is copied.‘

12.}

13.// Set dependencies with live collection of tasks.

14.allCopy.dependsOn project.tasks.matching {

15.it.name.startsWith(‘copy‘)

16.}

17.

18.// Add dependency tasks.

19.5.times {

20.// Dependencies for compileAll task.

21.task "compile${it}" << { println ‘Compile the code.‘ }

22.

23.// Dependencies for copyAll task.

24.task "copy${it}" << { println ‘Copy something.‘ }

25.}

If we invoke both tasks with Gradle we get the following output:

:compile0

Compile the code.

:compile1

Compile the code.

:compile2

Compile the code.

:compile3

Compile the code.

:compile4

Compile the code.

:allCompile

All is compiled.

:copy0

Copy something.

:copy1

Copy something.

:copy2

Copy something.

:copy3

Copy something.

:copy4

Copy something.

:allCopy

Everything is copied.

BUILD SUCCESSFUL

Total time: 2.258 secs

This blog post is based on Gradle version 1.0-rc-3

时间: 2024-10-05 10:29:22

Gradle Goodness: Working with Live Task Collection的相关文章

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

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: 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: 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: Using and Working with Gradle Version

To get the current Gradle version we can use the gradleVersion property of the Gradle object. This returns a string value we can use for displaying the values. If we want to compare Gradle versions we can use the GradleVersion object. With this class

从零开始学习Gradle之二---如何使用Task

上一篇文章中,我们提到了Gradle的一些基本概念,如Project.Task以及Action,并且创建了我们的第一个Task.这次我们来看看Gradle中关于Project和Task的更多细节. 1. Project和Task 对于build.gradle配置文件,当运行Gradle <Task> 时,Gradle会为我们创建一个Project的对象,来映射build.gradle中的内容.其中呢,对于不属于任何Task范畴的代码,Gradle会创建一个Script类的对象,来执行这些代码:

【转】从零开始学习Gradle之二---如何使用Task

原文:http://www.blogjava.net/wldandan/archive/2012/07/05/382246.html 上一篇文章中,我们提到了Gradle的一些基本概念,如Project.Task以及Action,并且创建了我们的第一个Task.这次我们来看看Gradle中关于Project和Task的更多细节. 1. Project和Task 对于build.gradle配置文件,当运行Gradle <Task> 时,Gradle会为我们创建一个Project的对象,来映射b

Gradle Goodness: Check Task Dependencies With a Dry Run

We can run a Gradle build without any of the task actions being executed. This is a so-called dry run of our build. We can use the dry run of a build to see if the task dependencies we have defined or are defined in a plugin are defined properly. Bec