From 6f18050729f77a61ff648cc7b9c102f61fd6c6b0 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
Date: Tue, 31 Oct 2000 12:13:14 +0000
Subject: [PATCH] Added an output attribute to .
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268135 13f79535-47bb-0310-9956-ffa450edef68
---
WHATSNEW | 6 +-
docs/index.html | 5 ++
.../tools/ant/taskdefs/ExecuteJava.java | 24 +++++++
.../org/apache/tools/ant/taskdefs/Java.java | 71 +++++++++++++------
4 files changed, 83 insertions(+), 23 deletions(-)
diff --git a/WHATSNEW b/WHATSNEW
index 6167a53bf..3be85d088 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -4,14 +4,16 @@ Changes from Ant 1.2 to the current sources
Other changes:
--------------
-* New tasks: propertyfile
+* New tasks: propertyfile, depend
+
+* Added output attribute to .
Fixed bugs:
-----------
* doesn't use deprectated methods anymore.
-* documentation of javadoc has been corrected.
+* javadoc's failonerror attribute works again
Changes from Ant 1.1 to Ant 1.2
===============================
diff --git a/docs/index.html b/docs/index.html
index 7c7e696de..131de72d7 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -2516,6 +2516,11 @@ the one that is currently running Ant.
fork is disabled)
No |
+
+ output |
+ Name of a file to write the output to. |
+ No |
+
Parameters specified as nested elements
arg and jvmarg
diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java b/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
index 3be7020b7..b8a8c3259 100644
--- a/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
+++ b/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
@@ -64,16 +64,19 @@ import org.apache.tools.ant.types.Path;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.io.*;
/*
*
* @author thomas.haas@softwired-inc.com
+ * @author Stefan Bodewig
*/
public class ExecuteJava {
private Commandline javaCommand = null;
private Path classpath = null;
private CommandlineJava.SysProperties sysProperties = null;
+ private PrintStream out;
public void setJavaCommand(Commandline javaCommand) {
this.javaCommand = javaCommand;
@@ -87,7 +90,18 @@ public class ExecuteJava {
sysProperties = s;
}
+ /**
+ * All output (System.out as well as System.err) will be written
+ * to this Stream.
+ */
+ public void setOutput(PrintStream out) {
+ this.out = out;
+ }
+
public void execute(Project project) throws BuildException{
+ PrintStream sOut = System.out;
+ PrintStream sErr = System.err;
+
final String classname = javaCommand.getExecutable();
final Object[] argument = { javaCommand.getArguments() };
try {
@@ -95,6 +109,11 @@ public class ExecuteJava {
sysProperties.setSystem();
}
+ if (out != null) {
+ System.setErr(out);
+ System.setOut(out);
+ }
+
final Class[] param = { Class.forName("[Ljava.lang.String;") };
Class target = null;
if (classpath == null) {
@@ -123,6 +142,11 @@ public class ExecuteJava {
if (sysProperties != null) {
sysProperties.restoreSystem();
}
+ if (out != null) {
+ System.setOut(sOut);
+ System.setErr(sErr);
+ out.close();
+ }
}
}
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/Java.java b/src/main/org/apache/tools/ant/taskdefs/Java.java
index f928bc561..88b2d0dc3 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Java.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Java.java
@@ -60,8 +60,7 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.*;
-import java.io.File;
-import java.io.IOException;
+import java.io.*;
import java.lang.reflect.*;
import java.util.*;
@@ -70,12 +69,14 @@ import java.util.*;
* for the called application thus resulting in much faster operation.
*
* @author Stefano Mazzocchi stefano@apache.org
+ * @author Stefan Bodewig
*/
public class Java extends Task {
private CommandlineJava cmdl = new CommandlineJava();
private boolean fork = false;
private File dir = null;
+ private File out;
private boolean failOnError = false;
/**
@@ -221,6 +222,13 @@ public class Java extends Task {
this.dir = d;
}
+ /**
+ * File the output of the process is redirected to.
+ */
+ public void setOutput(File out) {
+ this.out = out;
+ }
+
/**
* -mx or -Xmx depending on VM version
*/
@@ -241,6 +249,13 @@ public class Java extends Task {
exe.setJavaCommand(command.getJavaCommand());
exe.setClasspath(command.getClasspath());
exe.setSystemProperties(command.getSystemProperties());
+ if (out != null) {
+ try {
+ exe.setOutput(new PrintStream(new FileOutputStream(out)));
+ } catch (IOException io) {
+ throw new BuildException(io, location);
+ }
+ }
exe.execute(project);
}
@@ -249,27 +264,41 @@ public class Java extends Task {
* Executes the given classname with the given arguments in a separate VM.
*/
private int run(String[] command) throws BuildException {
- Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
+ FileOutputStream fos = null;
+ try {
+ Execute exe = null;
+ if (out == null) {
+ exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
Project.MSG_WARN),
null);
-
-
- exe.setAntRun(project);
-
- if (dir == null) {
- dir = project.getBaseDir();
- } else if (!dir.exists() || !dir.isDirectory()) {
- throw new BuildException(dir.getAbsolutePath()+" is not a valid directory",
- location);
- }
-
- exe.setWorkingDirectory(dir);
-
- exe.setCommandline(command);
- try {
- return exe.execute();
- } catch (IOException e) {
- throw new BuildException(e, location);
+ } else {
+ fos = new FileOutputStream(out);
+ exe = new Execute(new PumpStreamHandler(fos), null);
+ }
+
+ exe.setAntRun(project);
+
+ if (dir == null) {
+ dir = project.getBaseDir();
+ } else if (!dir.exists() || !dir.isDirectory()) {
+ throw new BuildException(dir.getAbsolutePath()+" is not a valid directory",
+ location);
+ }
+
+ exe.setWorkingDirectory(dir);
+
+ exe.setCommandline(command);
+ try {
+ return exe.execute();
+ } catch (IOException e) {
+ throw new BuildException(e, location);
+ }
+ } catch (IOException io) {
+ throw new BuildException(io, location);
+ } finally {
+ if (fos != null) {
+ try {fos.close();} catch (IOException io) {}
+ }
}
}