From 4bbf7cd116b09a68fb2de9ca43a1b4e981d0b385 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Mon, 23 Jan 2023 22:22:59 +0100 Subject: [PATCH] [MENFORCER-415] Move error message to MojoException In multimodal project it is very difficult to find a reason of failed job. --- .../maven/plugins/enforcer/EnforceMojo.java | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java b/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java index 0694a7c0..fa01727e 100644 --- a/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java +++ b/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/EnforceMojo.java @@ -18,10 +18,9 @@ */ package org.apache.maven.plugins.enforcer; +import java.util.ArrayList; import java.util.Hashtable; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; @@ -228,9 +227,6 @@ public void execute() throws MojoExecutionException { } } - // messages with warn/error flag - Map messages = new LinkedHashMap<>(); - // create my helper PluginParameterExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(session, mojoExecution); EnforcerRuleHelper helper = new DefaultEnforcementRuleHelper(session, evaluator, log, container); @@ -241,7 +237,7 @@ public void execute() throws MojoExecutionException { failFast = false; } - boolean hasErrors = false; + List errorMessages = new ArrayList<>(); // go through each rule for (int ruleIndex = 0; ruleIndex < rulesList.size(); ruleIndex++) { @@ -252,36 +248,30 @@ public void execute() throws MojoExecutionException { executeRule(ruleIndex, ruleDesc, helper); } catch (EnforcerRuleError e) { String ruleMessage = createRuleMessage(ruleIndex, ruleDesc, EnforcerLevel.ERROR, e); - throw new MojoExecutionException(ruleMessage, e); + throw new MojoExecutionException(System.lineSeparator() + ruleMessage, e); } catch (EnforcerRuleException e) { String ruleMessage = createRuleMessage(ruleIndex, ruleDesc, level, e); if (failFast && level == EnforcerLevel.ERROR) { - throw new MojoExecutionException(ruleMessage, e); + throw new MojoExecutionException(System.lineSeparator() + ruleMessage, e); } if (level == EnforcerLevel.ERROR) { - hasErrors = true; - messages.put(ruleMessage, true); + errorMessages.add(ruleMessage); } else { - messages.put(ruleMessage, false); + log.warn(ruleMessage); } } } - // log any messages - messages.forEach((message, error) -> { - if (fail && error) { - log.error(message); + if (!errorMessages.isEmpty()) { + if (fail) { + throw new MojoExecutionException( + System.lineSeparator() + String.join(System.lineSeparator(), errorMessages)); } else { - log.warn(message); + errorMessages.forEach(log::warn); } - }); - - if (fail && hasErrors) { - throw new MojoExecutionException( - "Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed."); } }