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.
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:allCompileAll is compiled.:copy0Copy something.:copy1Copy something.:copy2Copy something.:copy3Copy something.:copy4Copy something.:allCopyEverything is copied.BUILD SUCCESSFULTotal time: 2.232 secsWe 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:
:compile0Compile the code.:compile1Compile the code.:compile2Compile the code.:compile3Compile the code.:compile4Compile the code.:allCompileAll is compiled.:copy0Copy something.:copy1Copy something.:copy2Copy something.:copy3Copy something.:copy4Copy something.:allCopyEverything is copied.BUILD SUCCESSFULTotal time: 2.258 secsThis blog post is based on Gradle version 1.0-rc-3
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 Goodness: Copy Files with Filtering
Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- PHP 字符串常用操作
1,拼接字符串 拼接字符串是最常用到的字符串操作之一,在PHP中支持三种方式对字符串进行拼接操作,分别是圆点.分隔符{}操作,还有圆点等号.=来进行操作,圆点等号可以把一个比较长的字符串分解为几行进行 ...
- 建造者(生成器)模式C++、Java实现
1.建造者模式UML 图1. 建造者模式UML 2.C++实现 C++实现类视图: 图2. 建造者模式C++实现的类视图 其中,Product的实现代码是(ProductA和ProductB的代码不再 ...
- js中random的应用
1.生成一个随机数 var r = Math.random(); console.info(r); 结果生成一个0-1的随机数(返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1)) 2.生成 ...
- Windows下的代码注入
木马和病毒的好坏很大程度上取决于它的隐蔽性,木马和病毒本质上也是在执行程序代码,如果采用独立进程的方式需要考虑隐藏进程否则很容易被发现,在编写这类程序的时候可以考虑将代码注入到其他进程中,借用其他进程 ...
- mysql case when的使用
SELECT (CASE payType WHEN 1 THEN '微信' WHEN 2 THEN '支付宝' ELSE '余额' END) as type, count(payType) FROM ...
- JavaScript对HTML字符转义与反转义(转码和解码)
HTML的Encode(转码)和解码(Decode)在平时的开发中也是经常要处理的,在这里总结了使用javascript处理HTML的Encode(转码)和解码(Decode)的常用方式 一.用浏览器 ...
- CSS之inline和inline-block
inline-block 控制台-代码: PS:inline-block是让元素以内联形式存在,也就是不是块级,但是表现起来(又具有块级元素的高度)--也就是可以调高度(margin或者padding ...
- Csharp: speech to text, text to speech in win
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Django—Cookie and Session
一.Cookie Cookie,有时也用其复数形式 Cookies,指某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密). 1. 应用 服务器可以利用Co ...
- OpenCV中Mat属性step,size,step1,elemSize,elemSize1
Mat的step,size,step1,elemSize,elemSize1这几个属性非常容易混淆. OpenCV的官方参考手册也没有解释清楚这几个概念. 前一段时间研究了一下每个属性的含义,如果有什 ...