From 6d5f1bdc6b4f390a347af99ead32a1a0729e8041 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Sat, 20 Mar 2021 19:00:36 +0100 Subject: [PATCH] add discardOutput and discardError to redirector, apply, exec, java --- WHATSNEW | 5 ++ manual/Tasks/apply.html | 21 ++++++ manual/Tasks/exec.html | 21 ++++++ manual/Tasks/java.html | 21 ++++++ manual/Types/redirector.html | 21 ++++++ .../apache/tools/ant/taskdefs/ExecTask.java | 30 +++++++++ .../org/apache/tools/ant/taskdefs/Java.java | 30 +++++++++ .../apache/tools/ant/taskdefs/Redirector.java | 64 ++++++++++++++++++- .../tools/ant/util/NullOutputStream.java | 53 +++++++++++++++ src/tests/antunit/taskdefs/exec/exec-test.xml | 27 ++++++++ 10 files changed, 291 insertions(+), 2 deletions(-) create mode 100644 src/main/org/apache/tools/ant/util/NullOutputStream.java diff --git a/WHATSNEW b/WHATSNEW index 9d3f7c0f1..2036b94a2 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -69,6 +69,11 @@ Other changes: task. Bugzilla Report 65089 + * added new disardOutput and discardError properties to redirector + and the exec, apply and java tasks which can be used to completely + discard any (error) output. This is a platform independent + alternative to directiong output to any kind of null device. + Changes from Ant 1.10.8 TO Ant 1.10.9 ===================================== diff --git a/manual/Tasks/apply.html b/manual/Tasks/apply.html index b77cd7671..fad38c073 100644 --- a/manual/Tasks/apply.html +++ b/manual/Tasks/apply.html @@ -258,6 +258,27 @@ standard input.

Whether to bypass timestamp comparisons for target files. Since Ant 1.6.3. No; default is false + + discardOutput + Whether output should completely be discarded. This setting is + incompatible with any setting that redirects output to files or + properties.
+ If you set this to true error output will be discared as + well unless you redirect error output to files, properties or + enable logError. + Since Ant 1.10.10 + + No; defaults to false + + + discardError + Whether error output should completely be discarded. This + setting is incompatible with any setting that redirects error + output to files or properties as well as logError. + Since Ant 1.10.10 + + No; defaults to false +

Parameters specified as nested elements

fileset

diff --git a/manual/Tasks/exec.html b/manual/Tasks/exec.html index b298a65ec..05eb9a5b1 100644 --- a/manual/Tasks/exec.html +++ b/manual/Tasks/exec.html @@ -262,6 +262,27 @@ standard input.

when resolving the location of the executable. since Ant 1.6.3 No; default is false + + discardOutput + Whether output should completely be discarded. This setting is + incompatible with any setting that redirects output to files or + properties.
+ If you set this to true error output will be discared as + well unless you redirect error output to files, properties or + enable logError. + Since Ant 1.10.10 + + No; defaults to false + + + discardError + Whether error output should completely be discarded. This + setting is incompatible with any setting that redirects error + output to files or properties as well as logError. + Since Ant 1.10.10 + + No; defaults to false +

Examples

diff --git a/manual/Tasks/java.html b/manual/Tasks/java.html
index 98c4bd36b..31f4f0143 100644
--- a/manual/Tasks/java.html
+++ b/manual/Tasks/java.html
@@ -218,6 +218,27 @@ because it tries to read from the standard input.

forked JVM will be the same as those of the JVM running Ant. since Ant 1.7 No; default is false, ignored if fork is false + + discardOutput + Whether output should completely be discarded. This setting is + incompatible with any setting that redirects output to files or + properties.
+ If you set this to true error output will be discared as + well unless you redirect error output to files, properties or + enable logError. + Since Ant 1.10.10 + + No; defaults to false + + + discardError + Whether error output should completely be discarded. This + setting is incompatible with any setting that redirects error + output to files or properties as well as logError. + Since Ant 1.10.10 + + No; defaults to false +

Parameters specified as nested elements

arg and jvmarg

diff --git a/manual/Types/redirector.html b/manual/Types/redirector.html index c0a1c0eed..94ecd4852 100644 --- a/manual/Types/redirector.html +++ b/manual/Types/redirector.html @@ -129,6 +129,27 @@ specify source (input) and destination (output/error) files.

No; default is false + + discardOutput + Whether output should completely be discarded. This setting is + incompatible with any setting that redirects output to files or + properties.
+ If you set this to true error output will be discared as + well unless you redirect error output to files, properties or + enable logError. + Since Ant 1.10.10 + + No; defaults to false + + + discardError + Whether error output should completely be discarded. This + setting is incompatible with any setting that redirects error + output to files or properties as well as logError. + Since Ant 1.10.10 + + No; defaults to false +

Parameters specified as nested elements

inputmapper

diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java index e92536974..eebf6ae12 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java @@ -380,6 +380,36 @@ public class ExecTask extends Task { incompatibleWithSpawn |= append; } + /** + * Whether output should be discarded. + * + *

Defaults to false.

+ * + * @param discard + * if true output streams are discarded. + * + * @since Ant 1.10.10 + * @see #setDiscardError + */ + public void setDiscardOutput(final boolean discard) { + redirector.setDiscardOutput(discard); + } + + /** + * Whether error output should be discarded. + * + *

Defaults to false.

+ * + * @param discard + * if true error streams are discarded. + * + * @since Ant 1.10.10 + * @see #setDiscardOutput + */ + public void setDiscardError(final boolean discard) { + redirector.setDiscardError(discard); + } + /** * Add a RedirectorElement to this task. * diff --git a/src/main/org/apache/tools/ant/taskdefs/Java.java b/src/main/org/apache/tools/ant/taskdefs/Java.java index 7acdad4e0..7c55bbe6e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Java.java +++ b/src/main/org/apache/tools/ant/taskdefs/Java.java @@ -707,6 +707,36 @@ public class Java extends Task { incompatibleWithSpawn |= append; } + /** + * Whether output should be discarded. + * + *

Defaults to false.

+ * + * @param discard + * if true output streams are discarded. + * + * @since Ant 1.10.10 + * @see #setDiscardError + */ + public void setDiscardOutput(final boolean discard) { + redirector.setDiscardOutput(discard); + } + + /** + * Whether error output should be discarded. + * + *

Defaults to false.

+ * + * @param discard + * if true error streams are discarded. + * + * @since Ant 1.10.10 + * @see #setDiscardOutput + */ + public void setDiscardError(final boolean discard) { + redirector.setDiscardError(discard); + } + /** * Set the timeout in milliseconds after which the process will be killed. * diff --git a/src/main/org/apache/tools/ant/taskdefs/Redirector.java b/src/main/org/apache/tools/ant/taskdefs/Redirector.java index ee91adbd1..9950e9a05 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Redirector.java +++ b/src/main/org/apache/tools/ant/taskdefs/Redirector.java @@ -45,6 +45,7 @@ import org.apache.tools.ant.util.KeepAliveOutputStream; import org.apache.tools.ant.util.LazyFileOutputStream; import org.apache.tools.ant.util.LeadPipeInputStream; import org.apache.tools.ant.util.LineOrientedOutputStreamRedirector; +import org.apache.tools.ant.util.NullOutputStream; import org.apache.tools.ant.util.OutputStreamFunneler; import org.apache.tools.ant.util.ReaderInputStream; import org.apache.tools.ant.util.TeeOutputStream; @@ -197,6 +198,11 @@ public class Redirector { /** Is the output binary or can we safely split it into lines? */ private boolean outputIsBinary = false; + /** Flag which indicates if error and output files are to be discarded. */ + private boolean discardOut = false; + + private boolean discardErr = false; + /** * Create a redirector instance for the given task * @@ -442,6 +448,40 @@ public class Redirector { } } + /** + * Whether output should be discarded. + * + *

Defaults to false.

+ * + * @param discard + * if true output streams are discarded. + * + * @since Ant 1.10.10 + * @see #setDiscardError + */ + public void setDiscardOutput(final boolean discard) { + synchronized (outMutex) { + discardOut = discard; + } + } + + /** + * Whether error output should be discarded. + * + *

Defaults to false.

+ * + * @param discard + * if true error streams are discarded. + * + * @since Ant 1.10.10 + * @see #setDiscardOutput + */ + public void setDiscardError(final boolean discard) { + synchronized (errMutex) { + discardErr = discard; + } + } + /** * If true, (error and non-error) output will be "teed", redirected as * specified while being sent to Ant's logging mechanism as if no @@ -694,7 +734,17 @@ public class Redirector { /** outStreams */ private void outStreams() { - if (out != null && out.length > 0) { + final boolean haveOutputFiles = out != null && out.length > 0; + if (discardOut) { + if (haveOutputFiles || outputProperty != null) { + throw new BuildException("Cant discard output when output or outputProperty" + + " are set"); + } + managingTask.log("Discarding output", Project.MSG_VERBOSE); + outputStream = NullOutputStream.INSTANCE; + return; + } + if (haveOutputFiles) { final String logHead = "Output " + ((appendOut) ? "appended" : "redirected") + " to "; outputStream = foldFiles(out, logHead, Project.MSG_VERBOSE, @@ -716,7 +766,17 @@ public class Redirector { } private void errorStreams() { - if (error != null && error.length > 0) { + final boolean haveErrorFiles = error != null && error.length > 0; + if (discardErr) { + if (haveErrorFiles || errorProperty != null || logError) { + throw new BuildException("Cant discard error output when error, errorProperty" + + " or logError are set"); + } + managingTask.log("Discarding error output", Project.MSG_VERBOSE); + errorStream = NullOutputStream.INSTANCE; + return; + } + if (haveErrorFiles) { final String logHead = "Error " + ((appendErr) ? "appended" : "redirected") + " to "; errorStream = foldFiles(error, logHead, Project.MSG_VERBOSE, diff --git a/src/main/org/apache/tools/ant/util/NullOutputStream.java b/src/main/org/apache/tools/ant/util/NullOutputStream.java new file mode 100644 index 000000000..689ac495c --- /dev/null +++ b/src/main/org/apache/tools/ant/util/NullOutputStream.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.util; + +import java.io.OutputStream; + +/** + * OutputStream that completely discards all data written to it. + * + * @since Ant 1.10.10 + */ +public class NullOutputStream extends OutputStream { + + /** + * Shared instance which is safe to use concurrently as the stream + * doesn't hold any state at all. + */ + public static NullOutputStream INSTANCE = new NullOutputStream(); + + /** + * Doesn't do anything. + */ + @Override + public void write(byte[] b) { } + + /** + * Doesn't do anything. + */ + @Override + public void write(byte[] b, int off, int len) { } + + /** + * Doesn't do anything. + */ + @Override + public void write(int i) { } +} diff --git a/src/tests/antunit/taskdefs/exec/exec-test.xml b/src/tests/antunit/taskdefs/exec/exec-test.xml index c91db9bd7..bbe95e510 100644 --- a/src/tests/antunit/taskdefs/exec/exec-test.xml +++ b/src/tests/antunit/taskdefs/exec/exec-test.xml @@ -707,4 +707,31 @@ public class Hello { + + + + + + + + + + + + + + + + + + + + + + + + + + +