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.@Input
10.String splitOn
11. 
12.@InputFile
13.File xmlSource
14. 
15.// Output directory, will be created
16.// automatically if it doesn't exist yet.
17.@OutputDirectory
18.File destinationDir
19. 
20.@TaskAction
21.def splitXml() {
22.def slurper = new XmlParser().parse(xmlSource)
23. 
24.// Find all nodes where the tag name
25.// 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
:splitNames
 
BUILD SUCCESSFUL
 
Total time: 2.208 secs
$ ls build/splitter/names/
hubert.xml mrhaki.xml

Written with Gradle 1.2

 

Gradle Goodness: Task Output Annotations Create Directory Automatically的更多相关文章

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

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

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

  4. Gradle Goodness: Automatic Clean Tasks

    Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This ...

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

  6. Gradle Goodness: Skip Building Project Dependencies

    If we use Gradle in a multi-module project we can define project dependencies between modules. Gradl ...

  7. Gradle Goodness: Copy Files with Filtering

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

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

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

随机推荐

  1. 模块与包&常用模块

    一.模块的使用 模块定义:一系列功能的集合体 分为三大类:1.自定义模块 2.内置模块(比如 time,os,sys) 3.第三方模块 模块的表现形式: 1.使用python编写的py文件 2.已被编 ...

  2. Bzoj1498&1416: [NOI2006]神奇的口袋

    什么鬼双倍经验题??? Sol 考虑在第\(k\)次摸到\(y\)的概率 如果上次摸到\(y\),目前有\(sum\)个球,\(y\)有\(a[y]\)个,那么概率就是\(\frac{a[y]+d}{ ...

  3. sql中replace的用法

    update 表名 set 字段名=REPLACE (字段名,'原来的值','要修改的值') 如:将tbl_user表的user_name字段中的大写的A替换成小写的a update tbl_stud ...

  4. base64编码 的 图片 另存为下载

    功能描述: 有一段base64字符串的图片,将其保存下载为png图片! 可以:  直接 a 链接下载:  <a id="tttt" download="1.jpg& ...

  5. 纯css模仿天猫首页

    <style> *{margin:0;padding:0} li{list-style:none} a{text-decoration:none} #wrapper{font: 12px/ ...

  6. Jmeter运营活动并发测试—巧用集合点

    在运营活动测试过程中,经常需要对秒杀活动或定时抽奖活动进行并发测试.那么怎样快速便捷的模拟多用户同时参与活动,抽取奖品,进行并发测试呢?尤其是,当奖品总数N<用户总数M时,代码是否会存在奖品多发 ...

  7. arm汇编学习(六)---跳转到thumb状态

    通常函数返回使用 pop {r7,pc}或bx lr等方式(bx,b类似jmp为跳转指令,但bx可以指定跳转区域究竟为thumb还是arm指令.thumb指令指令的时候,直接填写该地址却总是产生SIG ...

  8. MySQL5.7的组提交与并行复制

    从MySQL5.5版本以后,开始引入并行复制的机制,是MySQL的一个非常重要的特性. MySQL5.6开始支持以schema为维度的并行复制,即如果binlog row event操作的是不同的sc ...

  9. [问题记录]cocos2dx编译打包apk过程&问题记录

    目录: 1. 入门 2. 编译 3. 问题 4. 总结 5. 参考 ------------------------------------------------------------------ ...

  10. VS断点不生效

    工程属性页中“配置属性”->“C/C++”->“常规”->“调试信息格式”,选择“用于“编辑并继承”的程序数据库(/ZI)”. 在“配置属性”->“链接器”->“调试”- ...