Skip to content

Commit 5a1ae9b

Browse files
authored
Merge pull request apache#206 from mfaquan/master
Throw BuildExceptions in ReplaceRegExp for IOExceptions and missing files
2 parents 5c89d9e + 0432a50 commit 5a1ae9b

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
* match="pattern"
7676
* replace="pattern"
7777
* flags="options"?
78-
* byline="true|false"? >
78+
* byline="true|false"?
79+
* failOnError="true|false"? >
7980
* regexp?
8081
* substitution?
8182
* fileset*
@@ -101,6 +102,10 @@
101102
* "true" indicates to perform replacement on a line by line basis
102103
* "false" indicates to perform replacement on the whole file at once.
103104
*
105+
* failOnError --> Should this task fail if an error occurs (default is false)
106+
* "true" indicates that this task should fail if an error occurs
107+
* "false" indicates that this task should continue if an error occurs
108+
*
104109
* Example:
105110
*
106111
* The following call could be used to replace an old property name in a ".properties"
@@ -123,6 +128,7 @@ public class ReplaceRegExp extends Task {
123128
private Union resources;
124129
private RegularExpression regex;
125130
private Substitution subs;
131+
private boolean failonerror = false;
126132

127133
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
128134

@@ -315,6 +321,15 @@ public void setPreserveLastModified(boolean b) {
315321
preserveLastModified = b;
316322
}
317323

324+
/**
325+
* If false, note errors but continue.
326+
*
327+
* @param failonerror true or false
328+
*/
329+
public void setFailOnError(boolean failonerror) {
330+
this.failonerror = failonerror;
331+
}
332+
318333
/**
319334
* Invoke a regular expression (r) on a string (input) using
320335
* substitutions (s) for a matching regex.
@@ -481,10 +496,17 @@ public void execute() throws BuildException {
481496
log("An error occurred processing file: '"
482497
+ file.getAbsolutePath() + "': " + e.toString(),
483498
Project.MSG_ERR);
499+
if (failonerror) {
500+
throw new BuildException("An error occurred processing file: '" + file.getAbsolutePath() + "'",
501+
e, getLocation());
502+
}
484503
}
485504
} else if (file != null) {
486505
log("The following file is missing: '"
487506
+ file.getAbsolutePath() + "'", Project.MSG_ERR);
507+
if (failonerror) {
508+
throw new BuildException("The following file is missing: '" + file.getAbsolutePath() + "'");
509+
}
488510
}
489511

490512
if (resources != null) {
@@ -498,10 +520,17 @@ public void execute() throws BuildException {
498520
log("An error occurred processing file: '"
499521
+ f.getAbsolutePath() + "': " + e.toString(),
500522
Project.MSG_ERR);
523+
if (failonerror) {
524+
throw new BuildException("An error occurred processing file: '" + f.getAbsolutePath() + "'",
525+
e, getLocation());
526+
}
501527
}
502528
} else {
503529
log("The following file is missing: '"
504530
+ f.getAbsolutePath() + "'", Project.MSG_ERR);
531+
if (failonerror) {
532+
throw new BuildException("The following file is missing: '" + f.getAbsolutePath() + "'");
533+
}
505534
}
506535
}
507536
}

0 commit comments

Comments
 (0)