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. FWORK-数据存储篇 -- 范式与反模式 (学习和理解)

    理解 1.第二范式的侧重点是非主键列是否完全依赖于主键,还是依赖于主键的一部分.第三范式的侧重点是非主键列是直接依赖于主键,还是直接依赖于非主键列.  2. 反模式 范式可以避免数据冗余,减少数据库的 ...

  2. IntelliJ IDEA 16创建Web项目

    首先要理解一个概念:在IntelliJ IDEA中“new Project”相当于eclipse中的工作空间(Workspace),而“new Module”相当于eclipse中的工程(Projec ...

  3. js-JavaScript的简介

    JavaScript的简介 * 是基于对象和事件驱动的语言,应用于客户端 - 基于对象: ** 提供好了很多对象,可以直接拿过来使用 - 事件驱动: ** HTML做网站静态效果,JavaScript ...

  4. es6的一些基本语法

    首先说一下什么是es6: ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准; let 和 const 命令 let的基本用法: 上面代码在代码块之中,分别用l ...

  5. Linux基础之命令练习Day2-useradd(mod,del),groupadd(mod,del),chmod,chown,

    作业一: 1) 新建用户natasha,uid为1000,gid为555,备注信息为“master” 2) 修改natasha用户的家目录为/Natasha 3) 查看用户信息配置文件的最后一行 4) ...

  6. EF+Oracle

    一个小项目,设计到几十张表,但都是简单的增删改查,所以呢,想偷懒用EF. 结果,在.NET4.0下,死活都不行.最后在Oracle官方找到demo,上面清清楚楚的写着必须>NET4.5. 看着E ...

  7. VStudio2015 开发MD风格的windows软件(附上使用第三方库教程)

    MD就是Google的一个安卓5.0+好看界面,都知道win下没有,那么就有大牛弄出了C#版的MD 特别说明:开发环境是.net 4.6 下面开始教程喂! 新建一个工程,这些不要我说了吧,如果没一点基 ...

  8. 浮动属性(float)

    (1.浮动是一种脱离标准文档流的形式. 作用:浮动就是用来制作多个盒子并排显示,也能设置宽高,负责网页排版 1 float:left;  左浮动 2 float:right; 右浮动 3 float: ...

  9. latex 调整间隔

    转自:http://blog.sina.com.cn/s/blog_4a4927c70100phsb.html,感谢分享! 一.LATEX调整公式与正文间距离,调整空白大小:   调整空白命令: \s ...

  10. 使用pdb调试python脚本

    pdb 是 python 自带的一个包,为 python 程序提供了一种交互的源代码调试功能,主要特性包括设置断点.单步调试.进入函数调试.查看当前代码.查看栈片段.动态改变变量的值等.pdb 提供了 ...