|
55 | 55 | import java.time.ZonedDateTime;
|
56 | 56 | import java.time.format.DateTimeFormatter;
|
57 | 57 | import java.time.format.DateTimeFormatterBuilder;
|
| 58 | +import java.util.ArrayList; |
58 | 59 | import java.util.LinkedList;
|
| 60 | +import java.util.List; |
59 | 61 | import java.util.Queue;
|
60 | 62 |
|
61 | 63 | import javax.xml.parsers.DocumentBuilder;
|
@@ -115,6 +117,18 @@ public class ReportMojo extends AbstractMojo {
|
115 | 117 | @Parameter(property = "report.fail.on.error", defaultValue = "true")
|
116 | 118 | private boolean failOnError;
|
117 | 119 |
|
| 120 | + /** |
| 121 | + * Describes of the build should fail if medium priority error is found |
| 122 | + */ |
| 123 | + @Parameter(property = "report.fail.on.warning", defaultValue = "false") |
| 124 | + private boolean failOnWarning; |
| 125 | + |
| 126 | + /** |
| 127 | + * Describes of the build should fail if low priority error is found |
| 128 | + */ |
| 129 | + @Parameter(property = "report.fail.on.debug", defaultValue = "false") |
| 130 | + private boolean failOnDebug; |
| 131 | + |
118 | 132 | /**
|
119 | 133 | * The directory where the summary report, containing links to the individual reports will be
|
120 | 134 | * generated
|
@@ -142,6 +156,14 @@ public void setFailOnError(boolean failOnError) {
|
142 | 156 | this.failOnError = failOnError;
|
143 | 157 | }
|
144 | 158 |
|
| 159 | + public void setFailOnWarning(boolean failOnWarning) { |
| 160 | + this.failOnWarning = failOnWarning; |
| 161 | + } |
| 162 | + |
| 163 | + public void setFailOnDebug(boolean failOnDebug) { |
| 164 | + this.failOnDebug = failOnDebug; |
| 165 | + } |
| 166 | + |
145 | 167 | public void setSummaryReport(File summaryReport) {
|
146 | 168 | this.summaryReportDirectory = summaryReport;
|
147 | 169 | }
|
@@ -222,8 +244,8 @@ public void execute() throws MojoFailureException {
|
222 | 244 | reportWarningsAndErrors(mergedReport, htmlOutputFileName);
|
223 | 245 | }
|
224 | 246 |
|
225 |
| - // 9. Fail the build if the option is enabled and high priority warnings are found |
226 |
| - if (failOnError) { |
| 247 | + // 9. Fail the build if any level error is enabled and configured error levels are found |
| 248 | + if (failOnError || failOnWarning || failOnDebug) { |
227 | 249 | failOnErrors(mergedReport);
|
228 | 250 | }
|
229 | 251 |
|
@@ -342,12 +364,21 @@ private int countPriority(NodeList messages, String priority) {
|
342 | 364 | }
|
343 | 365 |
|
344 | 366 | private void failOnErrors(File mergedReport) throws MojoFailureException {
|
345 |
| - int errorCount = selectNodes(mergedReport, "/sca/file/message[@priority=1]").getLength(); |
| 367 | + List<String> errorPriorities = new ArrayList<>(); |
| 368 | + addLevel(failOnError, "@priority=1", errorPriorities); |
| 369 | + addLevel(failOnWarning, "@priority=2", errorPriorities); |
| 370 | + addLevel(failOnDebug, "@priority=3", errorPriorities); |
| 371 | + String xpathExpression = "/sca/file/message[" + String.join(" or ", errorPriorities) + "]"; |
| 372 | + int errorCount = selectNodes(mergedReport, xpathExpression).getLength(); |
346 | 373 | if (errorCount > 0) {
|
347 |
| - throw new MojoFailureException(String.format( |
348 |
| - "%n" + "Code Analysis Tool has found %d error(s)! %n" |
349 |
| - + "Please fix the errors and rerun the build. %n", |
350 |
| - selectNodes(mergedReport, "/sca/file/message[@priority=1]").getLength())); |
| 374 | + throw new MojoFailureException(String.format("%n" + "Code Analysis Tool has found %d error(s)! %n" |
| 375 | + + "Please fix the errors and rerun the build. %n", errorCount)); |
| 376 | + } |
| 377 | + } |
| 378 | + |
| 379 | + private static void addLevel(boolean level, String levelValue, List<String> levels) { |
| 380 | + if (level) { |
| 381 | + levels.add(levelValue); |
351 | 382 | }
|
352 | 383 | }
|
353 | 384 |
|
|
0 commit comments