Browse Source

Add outputproperty attribute to <exec> to capture the output of the

external command in a property.

Submitted by:	Peter  Vogel <pvogel@arsin.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269400 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
f3b55df1fa
2 changed files with 37 additions and 0 deletions
  1. +6
    -0
      docs/manual/CoreTasks/exec.html
  2. +31
    -0
      src/main/org/apache/tools/ant/taskdefs/ExecTask.java

+ 6
- 0
docs/manual/CoreTasks/exec.html View File

@@ -50,6 +50,12 @@ systems.</p>
redirected.</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
command should be stored.</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


+ 31
- 0
src/main/org/apache/tools/ant/taskdefs/ExecTask.java View File

@@ -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) {}
}



Loading…
Cancel
Save