From c7a282f9415544a2ea5c946ecdb28fe465f7b6b7 Mon Sep 17 00:00:00 2001 From: Andrew Quan Date: Wed, 15 Nov 2023 14:36:57 -0500 Subject: [PATCH 1/2] Throw BuildExceptions in ReplaceRegExp for errors --- .../apache/tools/ant/taskdefs/optional/ReplaceRegExp.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java index c5b9d7d43a..524568cd38 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java @@ -481,10 +481,13 @@ public void execute() throws BuildException { log("An error occurred processing file: '" + file.getAbsolutePath() + "': " + e.toString(), Project.MSG_ERR); + throw new BuildException("An error occurred processing file: '" + file.getAbsolutePath() + "'", + e, getLocation()); } } else if (file != null) { log("The following file is missing: '" + file.getAbsolutePath() + "'", Project.MSG_ERR); + throw new BuildException("The following file is missing: '" + file.getAbsolutePath() + "'"); } if (resources != null) { @@ -498,10 +501,13 @@ public void execute() throws BuildException { log("An error occurred processing file: '" + f.getAbsolutePath() + "': " + e.toString(), Project.MSG_ERR); + throw new BuildException("An error occurred processing file: '" + f.getAbsolutePath() + "'", + e, getLocation()); } } else { log("The following file is missing: '" + f.getAbsolutePath() + "'", Project.MSG_ERR); + throw new BuildException("The following file is missing: '" + f.getAbsolutePath() + "'"); } } } From 0432a5009dee636f6fa24a6089b76e8a5bce4204 Mon Sep 17 00:00:00 2001 From: Andrew Quan Date: Thu, 16 Nov 2023 13:27:41 -0500 Subject: [PATCH 2/2] Add failonerror flag --- .../ant/taskdefs/optional/ReplaceRegExp.java | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java index 524568cd38..aca6d1ac68 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java @@ -75,7 +75,8 @@ * match="pattern" * replace="pattern" * flags="options"? - * byline="true|false"? > + * byline="true|false"? + * failOnError="true|false"? > * regexp? * substitution? * fileset* @@ -101,6 +102,10 @@ * "true" indicates to perform replacement on a line by line basis * "false" indicates to perform replacement on the whole file at once. * + * failOnError --> Should this task fail if an error occurs (default is false) + * "true" indicates that this task should fail if an error occurs + * "false" indicates that this task should continue if an error occurs + * * Example: * * The following call could be used to replace an old property name in a ".properties" @@ -123,6 +128,7 @@ public class ReplaceRegExp extends Task { private Union resources; private RegularExpression regex; private Substitution subs; + private boolean failonerror = false; private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); @@ -315,6 +321,15 @@ public void setPreserveLastModified(boolean b) { preserveLastModified = b; } + /** + * If false, note errors but continue. + * + * @param failonerror true or false + */ + public void setFailOnError(boolean failonerror) { + this.failonerror = failonerror; + } + /** * Invoke a regular expression (r) on a string (input) using * substitutions (s) for a matching regex. @@ -481,13 +496,17 @@ public void execute() throws BuildException { log("An error occurred processing file: '" + file.getAbsolutePath() + "': " + e.toString(), Project.MSG_ERR); - throw new BuildException("An error occurred processing file: '" + file.getAbsolutePath() + "'", - e, getLocation()); + if (failonerror) { + throw new BuildException("An error occurred processing file: '" + file.getAbsolutePath() + "'", + e, getLocation()); + } } } else if (file != null) { log("The following file is missing: '" + file.getAbsolutePath() + "'", Project.MSG_ERR); - throw new BuildException("The following file is missing: '" + file.getAbsolutePath() + "'"); + if (failonerror) { + throw new BuildException("The following file is missing: '" + file.getAbsolutePath() + "'"); + } } if (resources != null) { @@ -501,13 +520,17 @@ public void execute() throws BuildException { log("An error occurred processing file: '" + f.getAbsolutePath() + "': " + e.toString(), Project.MSG_ERR); - throw new BuildException("An error occurred processing file: '" + f.getAbsolutePath() + "'", - e, getLocation()); + if (failonerror) { + throw new BuildException("An error occurred processing file: '" + f.getAbsolutePath() + "'", + e, getLocation()); + } } } else { log("The following file is missing: '" + f.getAbsolutePath() + "'", Project.MSG_ERR); - throw new BuildException("The following file is missing: '" + f.getAbsolutePath() + "'"); + if (failonerror) { + throw new BuildException("The following file is missing: '" + f.getAbsolutePath() + "'"); + } } } }