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 ...
随机推荐
- jvm内置锁synchronized不能被中断
很久没看技术书籍了,今天看了一下<七周七并发模型>前面两章讲的java,写的还是有深度的.看到了一个有demo,说jvm内置锁synchronized是不能被中断的.照着书上写了个demo ...
- JS对象原型的理解
基于原型的语言 JavaScript 常被描述为一种基于原型的语言 (prototype-based language)——每个对象拥有一个原型对象,对象以其原型为模板.从原型继承方法和属性.原型对象 ...
- JavaScript 访问对象属性和方法及区别
这篇文章主要介绍了浅析JavaScript访问对象属性和方法及区别的相关资料,仅供参考 属性是一个变量,用来表示一个对象的特征,如颜色.大小.重量等:方法是一个函数,用来表示对象的操作,如奔跑.呼吸. ...
- html中块级元素和行内元素
块级元素和行内元素的三个区别 1.行内元素与块级元素直观上的区别: 行内元素会在一条直线上排列,都是同一行,水平方向排列 块级元素独占一行,垂直方向排列.块级元素从新行开始结束接着一个断行 2.块级元 ...
- SpringMVC学习(六)——@InitBinder注解
有些类型的数据是无法自动转换的,比如请求参数中包含时间类型的数据,无法自动映射到Controller里的Date参数.需要使用@initBinder注解为binder提供一个数据的转换器,这个转换器可 ...
- 密码存储中MD5的安全问题与替代方案
md5安全吗?有多么地不安全?如何才能安全地存储密码?... md5安全吗? 经过各种安全事件后,很多系统在存放密码的时候不会直接存放明文密码了,大都改成了存放了 md5 加密(hash)后的密码,可 ...
- C++ Deque(双向队列)
C++ Deque(双向队列)是一种优化了的.对序列两端元素进行添加和删除操作的基本序列容器.它允许较为快速地随机访问,但它不像vector 把所有的对象保存在一块连续的内存块,而是采用多个连续的 ...
- Hadoop学习---Ubuntu中hadoop完全分布式安装教程
软件版本 Hadoop版本号:hadoop-2.6.0-cdh5.7.0: VMWare版本号:VMware 9或10 Linux系统:CentOS 6.4-6.5 或Ubuntu版本号:ubuntu ...
- RedHat(Linux) Oracle数据库设置开机自启动
1 首先修改/etc/oratab文件添加如下行:ycr:/u01/app/oracle/product/12.1.0/dbhome_1:Y 关于/etc/oratab文件解释如下:# This fi ...
- Windows 10 X64 ISO 专业版&家庭版下载与永久激活
好久没有更新博客,算算时间,已经有4年了,好吧,今天突发奇想,想把今天安装Windows 10的过程给记录下来. 2015年的时候,微软就发布了Windows 10,当时正版的Win7.Win8都可以 ...