diff --git a/WHATSNEW b/WHATSNEW index 4fa1383e7..6720e8802 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -81,6 +81,10 @@ Other changes: * can optionally pass System.in to the remote process Bugzilla Report 55393 + * now supports capturing error output of the executed + process and setting a property from the return code. + Bugzilla Report 48478 + Changes from Ant 1.9.2 TO Ant 1.9.3 =================================== diff --git a/manual/Tasks/sshexec.html b/manual/Tasks/sshexec.html index 75f0faf19..2aab2a6bc 100644 --- a/manual/Tasks/sshexec.html +++ b/manual/Tasks/sshexec.html @@ -121,16 +121,35 @@ and won't work with versions of jsch earlier than since Ant 1.9.0 No, defaults to false + + suppresssystemerr + Whether to suppress system err. + since Ant 1.9.4 + No, defaults to false + output Name of a file to which to write the output. No + + errorOutput + The file to which the standard error of the + command should be redirected. since Ant 1.9.4 + No + append Whether output file should be appended to or overwritten. Defaults to false, meaning overwrite any existing file. No + + errAppend + Whether errorOutput file should be appended to or + overwritten. Defaults to false, meaning overwrite any existing + file. since Ant 1.9.4 + No + outputproperty The name of a property in which the output of the @@ -139,6 +158,19 @@ and won't work with versions of jsch earlier than command itself. No + + errorproperty + The name of a property in which the standard error of the + command should be stored. since Ant 1.9.4 + No + + + resultproperty + the name of a property in which the return code + of the command should be stored. Only of interest if + failonerror=false. since Ant 1.9.4 + No + timeout Stop the command if it doesn't finish within the diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java index d7f92a382..7f41b08f2 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java @@ -62,11 +62,15 @@ public class SSHExec extends SSHBase { private Thread thread = null; private String outputProperty = null; // like + private String errorProperty = null; + private String resultProperty = null; private File outputFile = null; // like + private File errorFile = null; private String inputProperty = null; private String inputString = null; // like private File inputFile = null; // like private boolean append = false; // like + private boolean appenderr = false; private boolean usePty = false; private boolean useSystemIn = false; @@ -76,9 +80,14 @@ public class SSHExec extends SSHBase { "Timeout period exceeded, connection dropped."; /** - * To supress writing logs to System.out + * To suppress writing logs to System.out */ private boolean suppressSystemOut = false; + + /** + * To suppress writing logs to System.err + */ + private boolean suppressSystemErr = false; /** * Constructor for SSHExecTask. @@ -125,6 +134,16 @@ public class SSHExec extends SSHBase { outputFile = output; } + /** + * If used, stores the erroutput of the command to the given file. + * + * @param output The file to write to. + * @since Apache Ant 1.9.4 + */ + public void setErrorOutput(File output) { + errorFile = output; + } + /** * If used, the content of the file is piped to the remote command * @@ -169,6 +188,18 @@ public class SSHExec extends SSHBase { public void setAppend(boolean append) { this.append = append; } + + /** + * Determines if the output is appended to the file given in + * setErrorOutput. Default is false, that is, overwrite + * the file. + * + * @param append True to append to an existing file, false to overwrite. + * @since Apache Ant 1.9.4 + */ + public void setErrAppend(boolean appenderr) { + this.appenderr = appenderr; + } /** * If set, the output of the command will be stored in the given property. @@ -179,6 +210,28 @@ public class SSHExec extends SSHBase { public void setOutputproperty(String property) { outputProperty = property; } + + /** + * If set, the erroroutput of the command will be stored in the given property. + * + * @param property The name of the property in which the command erroroutput + * will be stored. + * @since Apache Ant 1.9.4 + */ + public void setErrorproperty (String property) { + errorProperty = property; + } + + /** + * If set, the exitcode of the command will be stored in the given property. + * + * @param property The name of the property in which the exitcode + * will be stored. + * @since Apache Ant 1.9.4 + */ + public void setResultproperty(String property) { + resultProperty = property; + } /** * Whether a pseudo-tty should be allocated. @@ -207,6 +260,17 @@ public class SSHExec extends SSHBase { { this.suppressSystemOut = suppressSystemOut; } + + /** + * If suppressSystemErr is true, output will not be sent to System.err
+ * If suppressSystemErr is false, normal behavior + * @since Ant 1.9.4 + */ + public void setSuppressSystemErr(boolean suppressSystemErr) + { + this.suppressSystemErr = suppressSystemErr; + } + /** * Execute the command on the remote host. * @@ -290,6 +354,8 @@ public class SSHExec extends SSHBase { private void executeCommand(Session session, String cmd, StringBuffer sb) throws BuildException { ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream errout = new ByteArrayOutputStream(); + OutputStream teeErr = suppressSystemErr ? errout : new TeeOutputStream(out, KeepAliveOutputStream.wrapSystemErr()); OutputStream tee = suppressSystemOut ? out : new TeeOutputStream(out, KeepAliveOutputStream.wrapSystemOut()); InputStream istream = null ; @@ -326,6 +392,7 @@ public class SSHExec extends SSHBase { channel.setCommand(cmd); channel.setOutputStream(tee); channel.setExtOutputStream(tee); + channel.setErrStream(teeErr); if (istream != null) { channel.setInputStream(istream); } @@ -360,14 +427,25 @@ public class SSHExec extends SSHBase { log(TIMEOUT_MESSAGE, Project.MSG_ERR); } } else { - //success + // stdout to outputFile if (outputFile != null) { writeToFile(out.toString(), append, outputFile); } - + // set errorProperty + if (errorProperty != null) { + getProject().setNewProperty(errorProperty, errout.toString()); + } + // stderr to errorFile + if (errorFile != null) { + writeToFile(errout.toString(), appenderr, errorFile); + } // this is the wrong test if the remote OS is OpenVMS, // but there doesn't seem to be a way to detect it. int ec = channel.getExitStatus(); + // set resultproperty + if (resultProperty != null) { + getProject().setNewProperty(resultProperty, Integer.toString(ec)); + } if (ec != 0) { String msg = "Remote command failed with exit status " + ec; if (getFailonerror()) {