Search

Dark theme | Light theme

October 7, 2010

Gradle Goodness: Parse Output from Exec Invocation

We can run executable commands from Gradle build scripts with the exec() method. We pass a closure with the name of the executable and arguments to the method. The executable is than executed with the given arguments and we get a ExecResult object back. If we want to grap the output from the executable and use it for example to do some parsing we can assign a custom outputstream to the executable. The output of the executable is than written to our outputstream and when the process is finished we can manipulate the content of the outputstream.

In the following sample script we invoke svn info to get Subversion information. We parse the output and get the last revision number from the output.

// File: build.gradle
task svninfo << {
    new ByteArrayOutputStream().withStream { os ->
        def result = exec {
            executable = 'svn'
            args = ['info']
            standardOutput = os
        }
        def outputAsString = os.toString()
        def matchLastChangedRev = outputAsString =~ /Last Changed Rev: (\d+)/
        println "Latest Changed Revision #: ${matchLastChangedRev[0][1]}"
    }
}

// Example output for svn info:
// Path: .
// URL: http://svn.host/svn/project
// Repository Root: http://svn.host/svn/
// Repository UUID: 9de3ae54-a9c2-4644-a1a1-838cb992bc8e
// Revision: 33
// Node Kind: directory
// Schedule: normal
// Last Changed Author: mrhaki
// Last Changed Rev: 33
// Last Changed Date: 2010-09-03 14:25:41 +0200 (Fri, 03 Sep 2010)