diff --git a/WHATSNEW b/WHATSNEW index 8e2b0f0c2..07abf66c4 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -165,6 +165,9 @@ Other changes: * It is now possible to disable 's remote verification. Bugzilla report 35471. + * now supports input in a way similar to + Bugzilla report 39197. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/OptionalTasks/sshexec.html b/docs/manual/OptionalTasks/sshexec.html index 6df7607c1..ce214ab84 100644 --- a/docs/manual/OptionalTasks/sshexec.html +++ b/docs/manual/OptionalTasks/sshexec.html @@ -139,6 +139,26 @@ and won't work with versions of jsch earlier than Defaults to 0 which means "wait forever". No + + input + A file from which the executed command's standard + input is taken. This attribute is mutually exclusive with the + inputstring attribute.
+ When executing more than one command via commandRessource, input + will be read for each command. + since Ant 1.8.0 + No + + + inputstring + A string which serves as the input stream for the + executed command. This attribute is mutually exclusive with the + input attribute.
+ When executing more than one command via commandRessource, input + will be read for each command. + since Ant 1.8.0 + No +

Examples

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 163153953..d869437cc 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 @@ -19,10 +19,13 @@ package org.apache.tools.ant.taskdefs.optional.ssh; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; @@ -58,6 +61,8 @@ public class SSHExec extends SSHBase { private String outputProperty = null; // like private File outputFile = null; // like + private String inputProperty = null; // like + private File inputFile = null; // like private boolean append = false; // like private Resource commandResource = null; @@ -110,6 +115,24 @@ public class SSHExec extends SSHBase { outputFile = output; } + /** + * If used, the content of the file is piped to the remote command + * + * @param input The file which provides the input data for the remote command + */ + public void setInput(File input) { + inputFile = input; + } + + /** + * If used, the content of the property is piped to the remote command + * + * @param inputProperty The property which contains the input data for the remote command. + */ + public void setInputProperty(String inputProperty) { + this.inputProperty = inputProperty; + } + /** * Determines if the output is appended to the file given in * setOutput. Default is false, that is, overwrite @@ -151,6 +174,16 @@ public class SSHExec extends SSHBase { throw new BuildException("Command or commandResource is required."); } + if (inputFile != null && inputProperty != null) { + throw new BuildException("You can't specify both inputFile and" + + " inputProperty."); + } + if (inputFile != null && !inputFile.exists()) { + throw new BuildException("The input file " + + inputFile.getAbsolutePath() + + " does not exist."); + } + Session session = null; try { @@ -197,6 +230,25 @@ public class SSHExec extends SSHBase { ByteArrayOutputStream out = new ByteArrayOutputStream(); TeeOutputStream tee = new TeeOutputStream(out, new KeepAliveOutputStream(System.out)); + InputStream istream = null ; + if (inputFile != null) { + try { + istream = new FileInputStream(inputFile) ; + } catch (IOException e) { + // because we checked the existence before, this one + // shouldn't happen What if the file exists, but there + // are no read permissions? + log("Failed to read " + inputFile + " because of: " + + e.getMessage(), Project.MSG_WARN); + } + } + if (inputProperty != null) { + String inputData = getProject().getProperty(inputProperty) ; + if (inputData != null) { + istream = new ByteArrayInputStream(inputData.getBytes()) ; + } + } + try { final ChannelExec channel; session.setTimeout((int) maxwait); @@ -205,6 +257,9 @@ public class SSHExec extends SSHBase { channel.setCommand(cmd); channel.setOutputStream(tee); channel.setExtOutputStream(tee); + if (istream != null) { + channel.setInputStream(istream); + } channel.connect(); // wait for it to finish thread = @@ -275,7 +330,10 @@ public class SSHExec extends SSHBase { } else { log("Caught exception: " + e.getMessage(), Project.MSG_ERR); } + } finally { + FileUtils.close(istream); } + return out; }