This is an automated email from the ASF dual-hosted git repository. bodewig pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ant.git
The following commit(s) were added to refs/heads/master by this push: new 6d5f1bd add discardOutput and discardError to redirector, apply, exec, java 6d5f1bd is described below commit 6d5f1bdc6b4f390a347af99ead32a1a0729e8041 Author: Stefan Bodewig <bode...@apache.org> AuthorDate: Sat Mar 20 19:00:36 2021 +0100 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 +++++++ .../org/apache/tools/ant/taskdefs/ExecTask.java | 30 ++++++++++ src/main/org/apache/tools/ant/taskdefs/Java.java | 30 ++++++++++ .../org/apache/tools/ant/taskdefs/Redirector.java | 64 +++++++++++++++++++++- .../apache/tools/ant/util/NullOutputStream.java | 53 ++++++++++++++++++ src/tests/antunit/taskdefs/exec/exec-test.xml | 27 +++++++++ 10 files changed, 291 insertions(+), 2 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 9d3f7c0..2036b94 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 b77cd76..fad38c0 100644 --- a/manual/Tasks/apply.html +++ b/manual/Tasks/apply.html @@ -258,6 +258,27 @@ standard input.</p> <td>Whether to bypass timestamp comparisons for target files. <em>Since Ant 1.6.3</em>.</td> <td>No; default is <q>false</q></td> </tr> + <tr> + <td>discardOutput</td> + <td>Whether output should completely be discarded. This setting is + incompatible with any setting that redirects output to files or + properties.<br/> + If you set this to <q>true</q> error output will be discared as + well unless you redirect error output to files, properties or + enable <q>logError</q>. + <em>Since Ant 1.10.10</em> + </td> + <td>No; defaults to <q>false</q></td> + </tr> + <tr> + <td>discardError</td> + <td>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 <q>logError</q>. + <em>Since Ant 1.10.10</em> + </td> + <td>No; defaults to <q>false</q></td> + </tr> </table> <h3>Parameters specified as nested elements</h3> <h4>fileset</h4> diff --git a/manual/Tasks/exec.html b/manual/Tasks/exec.html index b298a65..05eb9a5 100644 --- a/manual/Tasks/exec.html +++ b/manual/Tasks/exec.html @@ -262,6 +262,27 @@ standard input.</p> when resolving the location of the executable. <em>since Ant 1.6.3</em></td> <td>No; default is <q>false</q></td> </tr> + <tr> + <td>discardOutput</td> + <td>Whether output should completely be discarded. This setting is + incompatible with any setting that redirects output to files or + properties.<br/> + If you set this to <q>true</q> error output will be discared as + well unless you redirect error output to files, properties or + enable <q>logError</q>. + <em>Since Ant 1.10.10</em> + </td> + <td>No; defaults to <q>false</q></td> + </tr> + <tr> + <td>discardError</td> + <td>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 <q>logError</q>. + <em>Since Ant 1.10.10</em> + </td> + <td>No; defaults to <q>false</q></td> + </tr> </table> <h3>Examples</h3> <pre> diff --git a/manual/Tasks/java.html b/manual/Tasks/java.html index 98c4bd3..31f4f01 100644 --- a/manual/Tasks/java.html +++ b/manual/Tasks/java.html @@ -218,6 +218,27 @@ because it tries to read from the standard input.</p> forked JVM will be the same as those of the JVM running Ant. <em>since Ant 1.7</em></td> <td>No; default is <q>false</q>, ignored if <var>fork</var> is <q>false</q></td> </tr> + <tr> + <td>discardOutput</td> + <td>Whether output should completely be discarded. This setting is + incompatible with any setting that redirects output to files or + properties.<br/> + If you set this to <q>true</q> error output will be discared as + well unless you redirect error output to files, properties or + enable <q>logError</q>. + <em>Since Ant 1.10.10</em> + </td> + <td>No; defaults to <q>false</q></td> + </tr> + <tr> + <td>discardError</td> + <td>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 <q>logError</q>. + <em>Since Ant 1.10.10</em> + </td> + <td>No; defaults to <q>false</q></td> + </tr> </table> <h3>Parameters specified as nested elements</h3> <h4>arg and jvmarg</h4> diff --git a/manual/Types/redirector.html b/manual/Types/redirector.html index c0a1c0e..94ecd48 100644 --- a/manual/Types/redirector.html +++ b/manual/Types/redirector.html @@ -129,6 +129,27 @@ specify source (input) and destination (output/error) files.</p> </td> <td>No; default is <q>false</q></td> </tr> + <tr> + <td>discardOutput</td> + <td>Whether output should completely be discarded. This setting is + incompatible with any setting that redirects output to files or + properties.<br/> + If you set this to <q>true</q> error output will be discared as + well unless you redirect error output to files, properties or + enable <q>logError</q>. + <em>Since Ant 1.10.10</em> + </td> + <td>No; defaults to <q>false</q></td> + </tr> + <tr> + <td>discardError</td> + <td>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 <q>logError</q>. + <em>Since Ant 1.10.10</em> + </td> + <td>No; defaults to <q>false</q></td> + </tr> </table> <h3>Parameters specified as nested elements</h3> <h4>inputmapper</h4> diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java index e925369..eebf6ae 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java @@ -381,6 +381,36 @@ public class ExecTask extends Task { } /** + * Whether output should be discarded. + * + * <p>Defaults to false.</p> + * + * @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. + * + * <p>Defaults to false.</p> + * + * @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 <code>RedirectorElement</code> to this task. * * @param redirectorElement <code>RedirectorElement</code>. diff --git a/src/main/org/apache/tools/ant/taskdefs/Java.java b/src/main/org/apache/tools/ant/taskdefs/Java.java index 7acdad4..7c55bbe 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Java.java +++ b/src/main/org/apache/tools/ant/taskdefs/Java.java @@ -708,6 +708,36 @@ public class Java extends Task { } /** + * Whether output should be discarded. + * + * <p>Defaults to false.</p> + * + * @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. + * + * <p>Defaults to false.</p> + * + * @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. * * @param value timeout in milliseconds. diff --git a/src/main/org/apache/tools/ant/taskdefs/Redirector.java b/src/main/org/apache/tools/ant/taskdefs/Redirector.java index ee91adb..9950e9a 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 * @@ -443,6 +449,40 @@ public class Redirector { } /** + * Whether output should be discarded. + * + * <p>Defaults to false.</p> + * + * @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. + * + * <p>Defaults to false.</p> + * + * @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 * redirection had taken place. Defaults to false. @@ -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 0000000..689ac49 --- /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 c91db9b..bbe95e5 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 { </au:assertTrue> </target> + <target name="test-discard-output" depends="setUp" if="test.can.run"> + <exec executable="sh" discardOutput="true"> + <arg value="parrot.sh" /> + <arg value="${ant.file}" /> + </exec> + <au:assertLogDoesntContain text="${ant.file} out" /> + <au:assertLogDoesntContain text="${ant.file} err" /> + </target> + + <target name="test-discard-output-only" depends="setUp" if="test.can.run"> + <exec executable="sh" discardOutput="true" logError="true"> + <arg value="parrot.sh" /> + <arg value="${ant.file}" /> + </exec> + <au:assertLogDoesntContain text="${ant.file} out" /> + <au:assertLogContains text="${ant.file} err" /> + </target> + + <target name="test-discard-error" depends="setUp" if="test.can.run"> + <exec executable="sh" discardError="true"> + <arg value="parrot.sh" /> + <arg value="${ant.file}" /> + </exec> + <au:assertLogContains text="${ant.file} out" /> + <au:assertLogDoesntContain text="${ant.file} err" /> + </target> + </project>