Skip to content

Commit

Permalink
Merge pull request apache#206 from mfaquan/master
Browse files Browse the repository at this point in the history
Throw BuildExceptions in ReplaceRegExp for IOExceptions and missing files
  • Loading branch information
bodewig authored Nov 16, 2023
2 parents 5c89d9e + 0432a50 commit 5a1ae9b
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
* match="pattern"
* replace="pattern"
* flags="options"?
* byline="true|false"? >
* byline="true|false"?
* failOnError="true|false"? >
* regexp?
* substitution?
* fileset*
Expand All @@ -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"
Expand All @@ -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();

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -481,10 +496,17 @@ public void execute() throws BuildException {
log("An error occurred processing file: '"
+ file.getAbsolutePath() + "': " + e.toString(),
Project.MSG_ERR);
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);
if (failonerror) {
throw new BuildException("The following file is missing: '" + file.getAbsolutePath() + "'");
}
}

if (resources != null) {
Expand All @@ -498,10 +520,17 @@ public void execute() throws BuildException {
log("An error occurred processing file: '"
+ f.getAbsolutePath() + "': " + e.toString(),
Project.MSG_ERR);
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);
if (failonerror) {
throw new BuildException("The following file is missing: '" + f.getAbsolutePath() + "'");
}
}
}
}
Expand Down

0 comments on commit 5a1ae9b

Please sign in to comment.