DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Gradle Goodness: Task Output Annotations Create Directory Automatically

Gradle Goodness: Task Output Annotations Create Directory Automatically

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Nov. 05, 12 · Interview
Like (0)
Save
Tweet
Share
9.04K Views

Join the DZone community and get the full member experience.

Join For 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

 

 

 

 

Task (computing) Directory Gradle Annotation

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best Practices for Writing Clean and Maintainable Code
  • How To Validate Three Common Document Types in Python
  • Apache Kafka vs. Memphis.dev
  • Memory Debugging: A Deep Level of Insight

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: