Hi all, my name is Hubert A. Klein Ikkink. Not a very common name, right? To make things easier I just picked the first letters of my firstname and surname and came up with haki. So there you have it, now I am also known as Mr. Haki or mrhaki for short. You can read more blog postings at www.mrhaki.com. I am a passionate Groovy and Java developer based in Tilburg, The Netherlands. My goal is to write clean, elegant, user-centered and high quality software. You can find me on Google+ and Twitter. Hubert is a DZone MVB and is not an employee of DZone and has posted 150 posts at DZone. You can read more from them at their website. View Full User Profile

Gradle Goodness: Task Output Annotations Create Directory Automatically

11.05.2012
| 5956 views |
  • submit to reddit

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.

task splitNames(type: SplitXmlTask) {
    xmlSource = file('src/xml/names.xml')
    destinationDir = file("$buildDir/splitter/names")
    splitOn = 'person'
}

defaultTasks 'splitNames'

class SplitXmlTask extends DefaultTask {
    @Input
    String splitOn

    @InputFile
    File xmlSource

    // Output directory, will be created
    // automatically if it doesn't exist yet.
    @OutputDirectory
    File destinationDir

    @TaskAction
    def splitXml() {
        def slurper = new XmlParser().parse(xmlSource)

        // Find all nodes where the tag name
        // equals the value for splitOn.
        // For each node we create a new file in 
        // the destinationDir directory with the 
        // complete XML node as contents.
        slurper.'**'.findAll { it.name() == splitOn }.each { node ->
            def outputFile = new File(destinationDir, "${node.name.text()}.xml")
            outputFile.withPrintWriter { writer ->
                writer.println '<?xml version="1.0"?>'
                new XmlNodePrinter(writer).print(node)
            }
        }
    }
}

Source for XML in src/xml/names.xml:

<?xml version="1.0"?>
<people>
    <person>
        <name>mrhaki</name>
        <country>The Netherlands</country>
    </person>
    <person>
        <name>hubert</name>
        <country>The Netherlands</country>
    </person>
</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

 

 

 

 

Published at DZone with permission of Hubert Klein Ikkink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)