zoukankan      html  css  js  c++  java
  • 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

     
  • 相关阅读:
    zend studio常见问题解答
    瀑布流插件(jquery.masonry.js)
    仿jQuery中undelegate()方法功能的函数
    Linux 常用命令
    linux debugfs 找回rm 的文件
    jq 添加和移除样式
    CentOS 搭建 nginx python django web server
    Linux vim 配置文件
    CentOS 安装python 3.3.2
    login.defs和shadow文件区别
  • 原文地址:https://www.cnblogs.com/GoAhead/p/4188054.html
Copyright © 2011-2022 走看看