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. canal —— 阿里巴巴mysql数据库binlog的增量订阅&消费组件

    阿里巴巴mysql数据库binlog的增量订阅&消费组件canal ,转载自  https://github.com/alibaba/canal 最新更新 canal QQ讨论群已经建立,群号 ...

  2. Java接口和抽象类理解(New)

    一. 抽象类和接口的特点  包含抽象方法的类称为抽象类,但并不意味着抽象类中只能有抽象方法,它和普通类一样,同样可以拥有成员变量和普通的成员方法.注意,抽象类和普通类的主要有三点区别: 1)抽象方法必 ...

  3. nginx关于 error_page指令详解.md

    error_page指令解释 nginx指令error_page的作用是当发生错误的时候能够显示一个预定义的uri,比如: error_page 502 503 /50x.html; 这样实际上产生了 ...

  4. oracle 基础笔记

    sqlplus / as sysbda;-- 运行命令conn scott/tiger [as sysdba];-- 登录show user;查看当前用户alter user scott accoun ...

  5. OpenCV 小图重叠至大图指定位置

    Android OpenCV Java: Codes: smallImg.copyTo( bigImg.submat( y, smallImg.rows(), x, smallImg.cols() ) ...

  6. Javascript之深入理解闭包

    闭包算是js里面比较不容易理解的点,尤其是对于没有编程基础的人来说. 其实闭包要注意的就那么几条,如果你都明白了那么征服它并不是什么难事儿.下面就让我们来谈一谈闭包的一些基本原理. 闭包的概念 一个闭 ...

  7. C++中精确到小数点后任意位

    #include <iostream> #include <iomanip> //用setprecision(n)设置精度,其中n表示精确到小数点后n位 using names ...

  8. mongodb 3.4 TAR包启动多个实例

    1:解压压缩文件 tar .tgz mkdir /home/maxiangqian/ mv mongodb /home/maxiangqian/ 2:加入环境变量 export PATH/bin:$P ...

  9. [问题记录]libpomelo编译报错:ssize_t重定义

    1. 时间:2015/01/16 描述:添加libpomelo到cocos2dx项目,报错如下图所示: 解决: 修改代码,源代码: #if !defined(_SSIZE_T_) && ...

  10. Python学习---线程锁/信号量/条件变量同步/线程池1221

    线程锁 问题现象: 多线程情况下,CPU遇到阻塞会进行线程的切换,所以导致执行了tmp-=1的值还未赋值给num=tmp,另一个线程2又开始了tmp -=1,所以导致最后的值重复赋值给了num,所以出 ...