Browse Source

various new attributes around System.err handling in sshexec. Submitted by Gilbert Rebhan. PR 48478

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1555866 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 11 years ago
parent
commit
93eb236577
3 changed files with 117 additions and 3 deletions
  1. +4
    -0
      WHATSNEW
  2. +32
    -0
      manual/Tasks/sshexec.html
  3. +81
    -3
      src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java

+ 4
- 0
WHATSNEW View File

@@ -81,6 +81,10 @@ Other changes:
* <sshexec> can optionally pass System.in to the remote process
Bugzilla Report 55393

* <sshexec> 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
===================================



+ 32
- 0
manual/Tasks/sshexec.html View File

@@ -121,16 +121,35 @@ and won't work with versions of jsch earlier than
<em>since Ant 1.9.0</em></td>
<td align="center" valign="top">No, defaults to false</td>
</tr>
<tr>
<td valign="top">suppresssystemerr</td>
<td valign="top">Whether to suppress system err.
<em>since Ant 1.9.4</em></td>
<td align="center" valign="top">No, defaults to false</td>
</tr>
<tr>
<td valign="top">output</td>
<td valign="top">Name of a file to which to write the output.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">errorOutput</td>
<td valign="top">The file to which the standard error of the
command should be redirected. <em>since Ant 1.9.4</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">append</td>
<td valign="top">Whether output file should be appended to or overwritten. Defaults to false, meaning overwrite any existing file.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">errAppend</td>
<td valign="top">Whether errorOutput file should be appended to or
overwritten. Defaults to false, meaning overwrite any existing
file. <em>since Ant 1.9.4</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">outputproperty</td>
<td valign="top">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.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">errorproperty</td>
<td valign="top">The name of a property in which the standard error of the
command should be stored. <em>since Ant 1.9.4</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">resultproperty</td>
<td valign="top">the name of a property in which the return code
of the command should be stored. Only of interest if
failonerror=false. <em>since Ant 1.9.4</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">timeout</td>
<td valign="top">Stop the command if it doesn't finish within the


+ 81
- 3
src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java View File

@@ -62,11 +62,15 @@ public class SSHExec extends SSHBase {
private Thread thread = null;

private String outputProperty = null; // like <exec>
private String errorProperty = null;
private String resultProperty = null;
private File outputFile = null; // like <exec>
private File errorFile = null;
private String inputProperty = null;
private String inputString = null; // like <exec>
private File inputFile = null; // like <exec>
private boolean append = false; // like <exec>
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
* <code>setErrorOutput</code>. 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 <code>true</code>, output will not be sent to System.err<br/>
* If suppressSystemErr is <code>false</code>, 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()) {


Loading…
Cancel
Save