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 ...
随机推荐
- maven filter不起作用
遇到的一个坑, spring boot + maven maven fileter没有起作用.spring boot把默认占位符改了 参考:https://blog.csdn.net/mn960mn/ ...
- poj3260 平衡问题(二维01背包)
http://www.cnblogs.com/ziyi--caolu/p/3228090.html http://blog.csdn.net/lyy289065406/article/details/ ...
- SZU 7
A - Megacity sqrtf是个坑 #include <iostream> #include <string> #include <cstring> #in ...
- Spring课程 Spring入门篇 6-1 Spring AOP API的PointCut、advice的概念及应用
本节主要是模拟spring aop 的过程. 实现spring aop的过程 这一节老师虽然说是以后在工作中不常用这些api,实际上了解还是有好处的, 我们可以从中模拟一下spring aop的过程. ...
- flask 服务器详解
#!/usr/local/bin/python # coding=utf-8 from flask import Flask app = Flask(__name__) @app.route('/') ...
- JavaScript的进阶之路(三)引用类型之Object类型和Array类型
引用类型 Object类型 function a(num){ if(num>3){ a(--num); } console.log(num); } a(5); //如何创建对象的实例 var o ...
- Java从入门到精通——数据库篇Mongo DB 安装启动及配置详解
一.概述 Mongo DB 下载下来以后我们应该如何去安装启动和配置才能使用Mongo DB,本篇博客就给大家讲述一下Mongo DB的安装启动及配置详解. 二.安装 1.下载Mongo DB ...
- ArcSDE 常用命令
一.sdeservice命令: 1. 创建sde服务:sdeservice –o create ArcSDE常用操作命令(转): 启动cmd 1. 创建和删除ArcSDE服务操作命令(sdeservi ...
- visual studio 2013的C++开发环境不错--vs2013安装试用手记
原文:http://blog.csdn.net/haoyujie/article/details/24370189 从visual studio 体系,最后一次对C++实现了大的改进,那还是vs 7. ...
- dedecms Ajax异步获取文章列表
dedecms如何通过ajax(异步)动态获取文章列表数据. 第一步添加:服务端(PHP)代码 打开plus目录下面的list.php文件,在12行代码下面添加以下代码: if(isset($_GET ...