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 generates files and the files have not changed than Gradle can skip the task. This speeds up the build process, which is good. If we write our own tasks we can use annotations for properties and methods to make them behave correctly for incremental build support. The @OutputDirectory annotation for example can be used for a property or method that defines a directory that is used by the task to put files in. The nice thing is that once we have designated such a directory as the output directory we don't have to write code to create the directory if it doesn't exist. Gradle will automatically create the directory if it doesn't exist yet. If we use the @OutputFile or @OutputFiles annotation the directory part of the file name is created if it doesn't exist.
In the following example build file we create a new task SplitXmlTask with the property destinationDir and we apply the @OutputDirectory annotation. If the directory doesn't exist Gradle will create it when we execute the task.
00.task splitNames(type: SplitXmlTask) {01.xmlSource = file('src/xml/names.xml')02.destinationDir = file("$buildDir/splitter/names")03.splitOn = 'person'04.}05. 06.defaultTasks 'splitNames'07. 08.class SplitXmlTask extends DefaultTask {09.@Input10.String splitOn11. 12.@InputFile13.File xmlSource14. 15.// Output directory, will be created16.// automatically if it doesn't exist yet.17.@OutputDirectory18.File destinationDir19. 20.@TaskAction21.def splitXml() {22.def slurper = new XmlParser().parse(xmlSource)23. 24.// Find all nodes where the tag name25.// equals the value for splitOn.26.// For each node we create a new file in 27.// the destinationDir directory with the 28.// complete XML node as contents.29.slurper.'**'.findAll { it.name() == splitOn }.each { node ->30.def outputFile = new File(destinationDir, "${node.name.text()}.xml")31.outputFile.withPrintWriter { writer ->32.writer.println '<?xml version="1.0"?>'33.new XmlNodePrinter(writer).print(node)34.}35.}36.}37.}Source for XML in src/xml/names.xml:
00.<?xml version="1.0"?>01.<people>02.<person>03.<name>mrhaki</name>04.<country>The Netherlands</country>05.</person>06.<person>07.<name>hubert</name>08.<country>The Netherlands</country>09.</person>10.</people>When we run the task and the build is successful we see two files in the directory build/splitter/names:
$ gradle:splitNamesBUILD SUCCESSFULTotal time: 2.208 secs$ ls build/splitter/names/hubert.xml mrhaki.xmlWritten with Gradle 1.2
Gradle Goodness: Task Output Annotations Create Directory Automatically的更多相关文章
- Gradle Goodness: Add Incremental Build Support to Custom Tasks with Annotations
In a previous post we learned how we can use the inputs and outputs properties to set properties or ...
- Gradle Goodness: Unpacking an Archive
To create an archive with Gradle is easy. We have several tasks like Zip, Tar, Jar, War and Ear to c ...
- 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: Automatic Clean Tasks
Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This ...
- 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 ...
- Gradle Goodness: Skip Building Project Dependencies
If we use Gradle in a multi-module project we can define project dependencies between modules. Gradl ...
- 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. ...
- [hadoop]Cannot create directory /mdrill/tablelist/fact_seller_all_d. Name node is in safe mode.
在执行mdrill创建表的时候报如下异常(蓝色部分为关键): [mdrill@hadoop1101 bin]$ ./bluewhale mdrill create ./create.sql higo ...
随机推荐
- 任意表格(table)实现拖动列(column)改变列大小
直接上代码吧,原理可以看我上一篇博文.本实现基于jquery,完美实现拖动改变表格的列大小功能,只需将代码放置在你页面的底部即可(jquery必须先引入). $(function () { var i ...
- Struts 2 框架相比Struts1的新特性
POJO形式和POJO动作 - 已经摆脱了Struts2的动作表单的Struts框架的一个组成部分.Struts2可以使用任何的POJO接收的形式输入.同样的,你现在可以看到任何POJO的Action ...
- C Primer Plus note6
error: invalid preprocessing directive #difine| 无效的宏定义处理 宏定义define 写成了 difine.
- UNION ALL 视图 'ImprotHIS2012.dbo.ImportHISData' 不可更新,因为没有找到分区依据列。 Severity 16 State 12
-- 3 更正措施,使约束check一次 Alter Table ImprotHIS_Bak_2011.dbo.ImportHISData with check Check Constraint al ...
- css-布局的漂浮
float ** 属性值 left: 文本流向对象的右边 right:文本流向对象的左边 <html> <head> <title>World</title& ...
- 第1章:程序设计和C语言(C语言入门)
一.程序和程序语言 1,程序的概念:完成某项事物所预设的活动方式. 2,程序设计:人们描述计算机要做的工作. 二 .程序设计语言及其发展 1.机器语言,2汇编语言,3高级语言{a)编译,b)解释}: ...
- SPOJ QTREE6
题意 给你一棵\(n\)个点的树,编号\(1\)~\(n\).每个点可以是黑色,可以是白色.初始时所有点都是黑色.有两种操作 \(0\ u\):询问有多少个节点\(v\)满足路径\(u\)到\(v\) ...
- easyui扩展行默认展开 以及 去除滚动条
问题背景: 在做打印页面的时候,要求有详细的默认展开显示. 遇到的问题: 1)在用扩展行的时候,grid的所有行都添加了展开收起的图标,(第二行没有明细)如下 2)默认展示有详细行的时候,内 ...
- Python基础-socket编程
一.网络编程 自从互联网诞生以来,现在基本上所有的程序都是网络程序,很少有单机版的程序了. 计算机网络就是把各个计算机连接到一起,让网络中的计算机可以互相通信.网络编程就是如何在程序中实现两台计算机的 ...
- JDBC连接数据库方法的封装,以及查询数据方法的封装
(在上一篇文章中,我们详细的介绍了连接数据库的方法,以及eclipse操作数据库信息的相关方法,在这里我们将主要讲封装.) 主要内容: 一般的连接数据库测试 把连接数据库的方法封装成一个类和测试 一个 ...