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 ...
随机推荐
- 六 Selector
选择器是java NIO中能够检测一到多个NIO通道(Channel),并能知晓是否为诸如读写时间做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接 为什么用Sele ...
- Tips——App启动速度的优化与监控
一.App的启动模式 冷启动:App点击启动前,它的进程不在系统里,需要系统新创建一个进程分配给它启动的情况.这是一次完整的启动过程. 热启动:App在冷启动后用户将App退后台,在App的进程还在系 ...
- js 闪动元素
<style> #div1{width:500px;height:100px;background:#888;font-size:5px;margin:0 auto;color:yello ...
- ORM(Object Relational Mapping)框架
ORM(Object Relational Mapping)框架 ORM(Object Relational Mapping)框架采用元数据来描述对象一关系映射细节,元数据一般采用XML格式,并且存放 ...
- Java——实现对密码进行MD5加密
package common; /** *@author作者 E-Mail: *@version 创建时间:2015-9-24+下午01:22:44 *类说明 **/ import java.secu ...
- 任务十五:零基础JavaScript编码(三)
任务目的 在上一任务基础上继续JavaScript的体验 接触一下JavaScript中的高级选择器 学习JavaScript中的数组对象遍历.读写.排序等操作 学习简单的字符串处理操作 任务描述 参 ...
- 深入理解mysql的底层实现
MySQL 的常用引擎 1. InnoDB InnoDB 的存储文件有两个,后缀名分别是 .frm 和 .idb,其中 .frm 是表的定义文件,而 idb 是数据文件. InnoDB 中存在表锁和行 ...
- 使用 Load Balancer,Corosync,Pacemaker 搭建 Linux 高可用集群
由于网络架构的原因,在一般虚拟机或物理环境中常见的用 VIP 来实现双机高可用方案,无法照搬到 Azure 平台.但利用 Azure 平台提供的负载均衡或者内部负载均衡功能,可以达到类似的效果. 本文 ...
- 从传输流收到意外的 EOF 或 0 个字节
/// <summary> /// 发送POST请求 /// </summary> /// <param name="json"></pa ...
- angular2 ng build --prod 报错:Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory'
调试页面 ng serve 正常 ng build 也正常 ng build --prod 异常:Module not found: Error: Can't resolve './$$_gendir ...