On 19/06/10 8:55 AM, Steve Ebersole wrote:
Hopefully this is still within the realm of user question :)

I have some code as part of a build that executes a call to `java
-version` via Runtime (I saw a bunch of "process" related classes in
Gradle source but could not see how to read back outputs from the
command).

The suggested way to do this is to use Project.exec { ... }. This method delegates to an ExecAction.

ExecAction has setStandardOutput() and setErrorOutput() methods which you can pass an InputStream to receive the child process' output:

Here's an example using exec { }

def stdout = new ByteArrayInputStream()
exec {
    commandLine('java', '-version')
    standardOutput = stdout
}

// now do something with stdout

If you don't have access to a Project instance, you can use DefaultExecAction directly:

ExecAction action = new DefaultExecAction()
action.commandLine('java', '-version')
def stdout = new ByteArrayInputStream()
action.setStandardOutput(stdout)
action.execute()

// do something with stdout

   Anyway, I launch this process and handle its streams (both
stdout and stderr) explicitly in my code.

Yet for some reason the process output is still being written to either
stdout or stderr of the gradle process.  Does Gradle perhaps plugin in
custom streams with System?  Basically I want to know how to not have
the output come to the gradle output.


Gradle does mess with System.out and err. But, looking at your code, I suspect the problem is that java -version is being run twice, once by the ExecHandle, which will write to System.out by default, and once by Runtime.exec(), which doesn't.

--
Adam Murdoch
Gradle Developer
http://www.gradle.org
CTO, Gradle Inc. - Gradle Training, Support, Consulting
http://www.gradle.biz


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Reply via email to