From f15a929e0ab1fbf1d65cc69914201a03981e617a Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Tue, 27 Nov 2001 19:25:44 +0000 Subject: [PATCH] PR 5025 : Execute still fails when failonerror=false and the error is program not found PR 4345 :Request for addition of "rcproperty" attribute to The property is called resultproperty; implements immutability in some code that should be replaced by a standalone method in Project. Documentation still TBD. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270032 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/ExecTask.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java index d29973ebb..4df75d96f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java @@ -92,6 +92,7 @@ public class ExecTask extends Task { private FileOutputStream fos = null; private ByteArrayOutputStream baos = null; private String outputprop; + private String resultProperty; /** Controls whether the VM (1.3 and above) is used to execute the command */ private boolean vmLauncher = true; @@ -177,6 +178,27 @@ public class ExecTask extends Task { return cmdl.createArgument(); } + /** + * fill a property in with a result. + * when no property is defined: failure to execute + * @since 1.5 + */ + public void setResultProperty(String resultProperty) { + this.resultProperty=resultProperty; + } + + /** + * helper method to set result property to the + * passed in value if appropriate + */ + protected void maybeSetResultPropertyValue(int result) { + String res=Integer.toString(result); + if(resultProperty!=null + && project.getProperty(resultProperty) == null) { + project.setProperty(resultProperty,res); + } + } + /** * Do the work. */ @@ -257,6 +279,7 @@ public class ExecTask extends Task { int err = -1; // assume the worst err = exe.execute(); + maybeSetResultPropertyValue(err); if (err != 0) { if (failOnError) { throw new BuildException(taskType + " returned: "+err, location); @@ -287,7 +310,11 @@ public class ExecTask extends Task { try { runExecute(exe); } catch (IOException e) { - throw new BuildException("Execute failed: " + e, e, location); + if (failOnError) { + throw new BuildException("Execute failed: ",e, location); + } else { + log("Execute failed: "+e.toString(), Project.MSG_ERR); + } } finally { // close the output file if required logFlush(); @@ -309,10 +336,9 @@ public class ExecTask extends Task { 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); + 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);