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.

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:

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

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

  1. Gradle Goodness: Task Output Annotations Create Directory Automatically

    Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of ...

  2. Gradle Goodness: Copy Files with Filtering

    Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...

  3. 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. ...

  4. 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 $ grad ...

  5. Gradle Goodness: Add Incremental Build Support to Tasks

    Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unle ...

  6. Gradle Goodness: Rename Ant Task Names When Importing Ant Build File

    Migrating from Ant to Gradle is very easy with the importBuild method from AntBuilder. We only have ...

  7. Gradle Goodness: Parse Files with SimpleTemplateEngine in Copy Task

    With the copy task of Gradle we can copy files that are parsed by Groovy's SimpleTemplateEngine. Thi ...

  8. 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 ru ...

  9. 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 r ...

随机推荐

  1. Java 集合类常用方法

    Collection中的contains()方法和remove()方法. boolean contains(Object o);该方法是用来判断集合中是否包含某个元素,若包含,返回true,不包含返回 ...

  2. 【原创】Hadoop的IO模型(数据序列化,文件压缩)

    数据序列化 我们知道,数据在分布式系统上运行程序数据是需要在机器之间通过网络传输的,这些数据必须被编码成一个个的字节才可以进行传输,这个其实就是我们所谓的数据序列化.数据中心中,最稀缺的资源就是网络带 ...

  3. flask之wtforms 表单验证(一)

    一  安装wtforms pip install wtforms 二  导入相关模块及对象 from wtforms import Form, widgets, validators from wtf ...

  4. git 永久性设置密码

    git 设置不需要输入密码 https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --g ...

  5. 洛谷P3960 列队(动态开节点线段树)

    题意 题目链接 Sol 看不懂splay..,看不懂树状数组... 只会暴力动态开节点线段树 观察之后不难发现,我们对于行和列需要支持的操作都是相同的:找到第\(k\)大的元素并删除,在末尾插入一个元 ...

  6. QQ 聊天机器人小薇 2.0.0 发布!

    本次发布主要加入了支持讨论组聊天,并增强了稳定性.另外,官方小薇 QQ 机器人已经下线,大家要体验的话请 自建私服~ 简介 XiaoV(小薇)是一个用 Java 写的 QQ 聊天机器人 Web 服务, ...

  7. 05_dubbo_aop

    [对这行代码进行源码分析] ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.c ...

  8. strcmp和stricmp、strcmpi三者之间的区别(C++)

    原文:http://www.cnblogs.com/tankeee/p/3957629.html #include <string.h> #include <stdio.h> ...

  9. 【Python】卸载完Python3 之后 Python2 无法打开IDLE

    安装官方的Python带Idle但是却无法打开,百度谷歌了几种解决方法,加上自己的实际境况予以解决. 我的python是直接安装在C盘下的. 1.首先是设置环境变量: Path=C:\Python27 ...

  10. [acm 1002] 浙大 Fire Net

    已转战浙大 题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 浙大acm 1002 #include <iostre ...