Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Gradle Goodness: Task Output Annotations Create Directory Automatically

DZone's Guide to

Gradle Goodness: Task Output Annotations Create Directory Automatically

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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

 

 

 

 

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}