• 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.@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

     
  • 相关阅读:
    程序员常见的口头禅,哈哈哈哈~
    2020年以后,IT行业还能否继续那么“吃香”?
    IntelliJ IDEA 15款 神级超级牛逼插件推荐(真的超级牛X)
    实验6——力导向图
    新的云端储存解决方案
    Router Mesh
    conda虚拟环境下,如果pip无法安装的包,可以试试下面这个命令
    Version value must be a string; got a NilClass () (TypeError)
    --use-feature=2020-resolver 检查环境冲突
    焦虑情绪
  • 原文地址:https://www.cnblogs.com/GoAhead/p/4188054.html
Copyright © 2020-2023  润新知