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. python对excel文件的读写操作

    import xlrd,xlwt data = xlrd.open_workbook('a.xlsx') #读 table = data.sheets()[0] data_list = [] data ...

  2. timestamp to time 时间戳转日期

    function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);   //timestamp 为10位需*100 ...

  3. js数组与字符串处理 slice、splice、substring、substr、push、pop、shift、reverse、sort、join、split

    数组 方法 1.在数组末尾添加.删除元素 push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返回修改后数组的长度.改变原数组 pop()方法则从数组末尾移除最后一个元素,减少数组的 ...

  4. HTML5 : 文件上传下载

    网站建设中,文件上传与下载在所难免,HTML5中提供的API在前端有着丰富的应用,完美的解决了各个浏览器的兼容性问题,所以赶紧get吧! FileList 对象和 file 对象 HTML 中的 in ...

  5. 关于输入框在谷歌浏览器 ie 浏览器中 黄色背景的去除

    谷歌有自己对input 的填充色 加上下面的css 就可以了 input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white ins ...

  6. 关于Function原型对象和Object原型对象的一些疑惑

    网上有一道美团外卖的面试题是这样的: Function.prototype.a = 'a'; Object.prototype.b = 'b'; function Person(){}; var p ...

  7. eclipse遇到的异常

    1.  Widget disposed too early for part com.kompakar.ehealth.ui.emr.mstr.medicaldocumentaudit.Medical ...

  8. Windows操作系统----锁住命令行窗口

    第一步: 新建一个.txt文档,输入如下内容: @echo off echo. setlocal :checkpassword set /p password=请输入密码: if "%pas ...

  9. javax.swing.JComponent 调用顺序

    网上截取的,感觉挺有用,记录下来. http://bbs.csdn.net/topics/310041707 java swing 感觉好复杂啊…………一点都不想用但是作业要用到 >_<; ...

  10. leetcode-palindrome partitioning-ZZ

    http://yucoding.blogspot.com/2013/08/leetcode-question-132-palindrome.html Analysis:When face the &q ...