Gradle goodness:task Output Annotations Create Directory automatically

Source: Internet
Author: User

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 was only executed if it is really necessary. For example if a task generates files and the files has not changed than Gradle can skip the task. This speeds to 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 is used for a property or method that defines a directory tha T is used by the task to put files in. The nice thing is and once we have designated such a directory as the output directory we don't have to write code to CRE Ate the directory if it doesn ' t exist. Gradle would 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 Crea Ted if it doesn ' t exist.

The following example build file we create a new task Splitxmltask with the property and destinationDir we apply the annotation. If the directory doesn ' t exist Gradle would create it when we execute the task.

View Sourceprint? 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 :

View Sourceprint? 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 are successful we see both 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.