Gradle Goodness: Task Output Annotations Create Directory Automatically
6782 단어 Annotations
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. view source
?
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 source
?
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SL4.DataAnnotations 데이터 검증 방법할 말 없어요. 아주 간단해요. 바로 코드로 가세요. XAML: XAML.CS: Users.CS:...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.