Search

Dark theme | Light theme

October 25, 2012

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.

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

Written with Gradle 1.2