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. Jvm性能监控和常用工具

    JDK常用命令行工具   Jps : jps [options] [hostid]  , -q 只显示jvmid, -m 传递给主类main的参数,-l 类全名,-v jvm启动参数 jstat : ...

  2. 学习笔记2_Day09_servlet的细节

    Servlet细节 l  不要在Servlet中创建成员!创建局部变量即可! l  可以创建无状态成员! l  可以创建有状态的成员,但状态必须为只读的! 1 Servlet与线程安全 因为一个类型的 ...

  3. OSGEarth加载 geoserver 发布 TMS

    geoserver配好数据并用自带的gwc切片好后, 访问 http://localhost:9999/geoserver/gwc/service/tms/1.0.0/ 在OsgEarth的earth ...

  4. 基于zxing的二维码(网格)扫描

    基于zxing的二维码(网格)扫描 前言:对于二维码扫描我们使用的是开源框架Zxing或者Zbar,这里使用基于zxing的二维码扫描,类似支付宝网格扫描, 二维码原理介绍: 二维码是用某种特定的几何 ...

  5. gitattributes中的filter

    .gitattributes文件就是一个简单的text文本文件,它的作用是gives attributes to pathnames. 该文件中的一些配置可以为某些特定目录或者文件来设置,这样Git就 ...

  6. SQL Server 2016 ->> T-SQL新特性

    1) TRUNCATE表分区而不是整表 CREATE TABLE dbo.TruncatePartitionTest ( PrtCol INT, Col2 ) ) ON [myPS1](PrtCol) ...

  7. 使用Powershell 管理 Windows 2012 hyper-v复制

    HyperV复制相关命令 Suspend-VMReplication Suspends replication of a virtual machine. 暂停复制虚拟机. Resume-VMRepl ...

  8. python全栈学习笔记(一)网络基础之网络协议篇

    阅读目录 一.操作系统基础 二.网络通信原理 2.1 互联网的本质就是一系列的网络协议 2.2 osi七层协议 2.3 tcp/ip五层模型讲解 2.3.1 物理层 2.3.2 数据链路层 2.3.3 ...

  9. liunx screen使用简单实验

    liunx screen使用 今天因工作需要使用到screen工具,感觉挺有意思,记录一下 GNU Screen是一款由GNU计划开发的用于命令行终端切换的自由软件.用户可以通过该软件同时连接多个本地 ...

  10. July 09th 2017 Week 28th Sunday

    He that boasts of his own knowledge proclaims ignorance. 夸耀知识实乃无知. Honestly speaking, I don't agree ...