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 ...
随机推荐
- cocos creator Touch事件应用(触控选择多个子节点)
最近参与了cocos creator的研究,开发小游戏,结果被一个事件坑得不行不行的.现在终于解决了,分享给大家. 原理 1.触控事件是针对节点的 2.触控事件的冒泡,是一级一级往上冒泡,中间可以阻止 ...
- EasyUI datebox 日期范围 日期关联
jQuery EasyUI 1.4.5 html: <label>提交日期:</label> <input id="startDate" name=& ...
- Node.js开发——MongoDB与Mongoose
为了保存网站的用户数据和业务数据,通常需要一个数据库.MongoDB和Node.js特别般配,因为MongoDB是基于文档的非关系型数据库,文档是按BSON(JSON的轻量化二进制格式)存储的,增删改 ...
- [国家集训队2012]middle(陈立杰)
我是萌萌的传送门 我是另一个萌萌的传送门 脑残错误毁一下午…… 其实题解早就烂大街了,然而很久之前我只知道是二分答案+主席树却想不出来这俩玩意儿怎么一块儿用的……今天又翻了几篇题解才恍然大悟,是把权值 ...
- Stirling数
第一类: 定义 第一类Stirling数表示表示将 n 个不同元素构成m个圆排列的数目.又根据正负性分为无符号第一类Stirling数 和带符号第一类Stirling数 .有无符号Stir ...
- ArcGIS DataStore手册——管理篇
第二章:ArcGIS DataStore管理维护 1.备份管理 备份的目的在于发生原始数据损坏或其他突发情况时,可避免数据丢失,并可快速的使用备份数据来恢复,以保证服务仍可使用. 单机模式下,可使用D ...
- 怎么区分odd和even
odd [ɒd] 和even ['iːv(ə)n] 一个表示奇数.一个表示偶数 经常混淆. 一个记住的好方法: odd是3个字母,单数,所以表示奇数 even是4个字母,所以表示偶数
- 浅析 if __name__ == __main__ :
有句话经典的概括了这段代码的意义: “Make a script both importable and executable” 意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可 ...
- 搭建git远程仓库
基于本地协议搭建git远程仓库 1.任意目录下执行git init -bare创建裸仓库,建议目录名称以.git结尾 2.共享此目录,windows下右键裸仓库目录,切换到共享面板设置完成即可获取共享 ...
- JAVA程序员常用开发工具
1.JDK (Java Development Kit)Java开发工具集 SUN的Java不仅提了一个丰富的语言和运行环境,而且还提了一个免费的Java开发工具集(JDK).开发人员和最终用户可以利 ...