Gradle Goodness: Task Output Annotations Create Directory Automatically
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.@Input10.String splitOn11. 12.@InputFile13.File xmlSource14. 15.// Output directory, will be created16.// automatically if it doesn't exist yet.17.@OutputDirectory18.File destinationDir19. 20.@TaskAction21.def splitXml() {22.def slurper = new XmlParser().parse(xmlSource)23. 24.// Find all nodes where the tag name25.// 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:splitNamesBUILD SUCCESSFULTotal time: 2.208 secs$ ls build/splitter/names/hubert.xml mrhaki.xmlWritten with Gradle 1.2
Gradle Goodness: Task Output Annotations Create Directory Automatically的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Gradle Goodness: Automatic Clean Tasks
Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This ...
- 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 ...
- Gradle Goodness: Skip Building Project Dependencies
If we use Gradle in a multi-module project we can define project dependencies between modules. Gradl ...
- Gradle Goodness: Copy Files with Filtering
Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...
- 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. ...
- [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 ...
随机推荐
- Tomcat服务器使用(二)
1. 打包Javaweb应用 当开发人员在自己的开发机器上调试所有代码并通过后,为了进行产品发布,都需要将开发人员的源码打包成war包进行发布.之前已经了解了jar包,那么war包和jar包的区别是什 ...
- https、公钥、私钥白话解说
原文 摘要:本文尝试一步步还原HTTPS的设计过程,以理解为什么HTTPS最终会是这副模样.但是这并不代表HTTPS的真实设计过程.在阅读本文时,你可以尝试放下已有的对HTTPS的理解,这样更利于“还 ...
- Java泛型拾遗
先上百度百科的解释 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方 ...
- Effective C++ .07 virtual析构函数的提供
主要讲了, 1. virtual析构函数的作用与调用顺序 2. 使用时机,并不是使用了继承就要把基类的析构函数变为虚函数(virtual),只有当用于多态目的时才进行一个virtual析构函数的定义. ...
- flask 继承模版的基本使用1
- GDAL安装和使用
1.安装 下载源程序包 ,解压,运行以下三条命令 ./configure --prefix=~ make make install
- 弧形菜单(Android)
弧形菜单(Android) 前言:公司需求,自己写的一个弧形菜单! 效果: 开发环境:AndroidStudio2.2.1+gradle-2.14.1 涉及知识:1.自定义控件,2.事件分发等 部分代 ...
- 十个免费的 Web 压力测试工具
本文列举了是十个免费工具,可以用来进行Web的负载/压力测试的.这样你就可以知道你的服务器以及你的WEB应用能够扛得住多少的并发量,以及网站性能. 0. Grinder – Grinder是一个开源 ...
- https填坑之旅
Boss说,我们买了个权威证书,不如做全站式的https吧,让用户打开主页就能看到受信任的绿标.于是我们就开始了填坑之旅. [只上主域好不好?] 不好...console会报出一大堆warning因为 ...
- python 图形界面
Python自带的库是支持Tk的Tkinter,使用Tkinter,无需安装任何包,就可以直接使用. Tk是一个图形库,支持多个操作系统 导入Tkinter包的所有内容: from tkinter i ...