From f3b55df1fabe81fe1edabb25903fa4f8c0a8b50b Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 27 Jul 2001 09:55:46 +0000 Subject: [PATCH] Add outputproperty attribute to to capture the output of the external command in a property. Submitted by: Peter Vogel git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269400 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/exec.html | 6 ++++ .../apache/tools/ant/taskdefs/ExecTask.java | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/docs/manual/CoreTasks/exec.html b/docs/manual/CoreTasks/exec.html index 219b1c0fb..d7364e9b0 100644 --- a/docs/manual/CoreTasks/exec.html +++ b/docs/manual/CoreTasks/exec.html @@ -50,6 +50,12 @@ systems.

redirected. No + + outputproperty + the name of a property in which the output of the + command should be stored. + No + timeout Stop the command if it doesn't finish within the diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java index be876ea0e..19d3363a3 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java @@ -70,6 +70,8 @@ import java.io.*; */ public class ExecTask extends Task { + private static String lSep = System.getProperty("line.separator"); + private String os; private File out; private File dir; @@ -79,6 +81,8 @@ public class ExecTask extends Task { private Environment env = new Environment(); protected Commandline cmdl = new Commandline(); private FileOutputStream fos = null; + private ByteArrayOutputStream baos = null; + private String outputprop; /** Controls whether the VM (1.3 and above) is used to execute the command */ private boolean vmLauncher = true; @@ -128,6 +132,14 @@ public class ExecTask extends Task { this.out = out; } + /** + * Property name whose value should be set to the output of + * the process + */ + public void setOutputproperty(String outputprop) { + this.outputprop = outputprop; + } + /** * Throw a BuildException if process returns non 0. */ @@ -243,6 +255,19 @@ public class ExecTask extends Task { log("Result: " + err, Project.MSG_ERR); } } + if (baos != null) { + BufferedReader in = + new BufferedReader(new StringReader(baos.toString())); + String line = null; + StringBuffer val = new StringBuffer(); + while ((line = in.readLine()) != null) { + if (val.length() != 0) { + val.append(lSep); + } + val.append(line); + } + project.setProperty(outputprop, val.toString()); + } } /** @@ -274,6 +299,11 @@ public class ExecTask extends Task { } catch (IOException ioe) { throw new BuildException("Cannot write to "+out, ioe, location); } + } else if (outputprop != null) { + // try { + baos = new ByteArrayOutputStream(); + log("Output redirected to ByteArray", Project.MSG_VERBOSE); + return new PumpStreamHandler(baos); } else { return new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN); @@ -294,6 +324,7 @@ public class ExecTask extends Task { protected void logFlush() { try { if (fos != null) fos.close(); + if (baos != null) baos.close(); } catch (IOException io) {} }