Gradle Goodness: Task Output Annotations Create Directory Automatically

Gradle任务输出目录自动创建
利用Gradle的@OutputDirectory注解特性,任务可以自动创建输出目录,无需额外编码。示例展示了如何通过SplitXmlTask任务将XML文件拆分为多个文件,并自动创建必要的输出目录。

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

 

转载于:https://www.cnblogs.com/GoAhead/p/4188054.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值