This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new e417e45e91 [MNG-8196] Make exception messages match Maven 3 again 
(#1628)
e417e45e91 is described below

commit e417e45e910bdd220479322ea78206c1542ebb00
Author: Stefan Oehme <st.oe...@gmail.com>
AuthorDate: Fri Aug 16 11:25:16 2024 +0200

    [MNG-8196] Make exception messages match Maven 3 again (#1628)
    
    * Make exception messages match Maven 3 again
    
    The boxing of `MojoException` into a `MojoExecutionException` brought two 
issues:
    
    1. It added another layer to the stacktrace, making it a bit less readable 
for users and breaking test expectations for Maven extension authors (this was 
my main motivation for this change)
    2. It lost the `longMessage`, which meant that this message was no longer 
shown at the end of the build.
    
    This change fixes both problems by passing through the original 
`MavenException` and treating this new exception type directly in 
`DefaultExceptionHandler` and `MojoExecutor`
    
    * Broaden to MavenException
    
    ---------
    
    Co-authored-by: Guillaume Nodet <gno...@gmail.com>
---
 .../maven/exception/DefaultExceptionHandler.java    | 21 +++++++++++++--------
 .../maven/lifecycle/internal/MojoExecutor.java      |  4 +++-
 .../maven/plugin/DefaultBuildPluginManager.java     | 14 ++++++--------
 3 files changed, 22 insertions(+), 17 deletions(-)

diff --git 
a/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java
 
b/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java
index 059e1c8e17..90b9c93276 100644
--- 
a/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java
+++ 
b/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java
@@ -30,6 +30,7 @@ import java.util.IdentityHashMap;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.maven.api.plugin.MojoException;
 import org.apache.maven.lifecycle.LifecycleExecutionException;
 import org.apache.maven.model.building.ModelProblem;
 import org.apache.maven.model.building.ModelProblemUtils;
@@ -232,16 +233,20 @@ public class DefaultExceptionHandler implements 
ExceptionHandler {
         Set<Throwable> dejaVu = Collections.newSetFromMap(new 
IdentityHashMap<>());
         for (Throwable t = exception; t != null && t != t.getCause(); t = 
t.getCause()) {
             String exceptionMessage = t.getMessage();
+            String longMessage = null;
 
             if (t instanceof AbstractMojoExecutionException) {
-                String longMessage = ((AbstractMojoExecutionException) 
t).getLongMessage();
-                if (longMessage != null && !longMessage.isEmpty()) {
-                    if ((exceptionMessage == null || 
exceptionMessage.isEmpty())
-                            || longMessage.contains(exceptionMessage)) {
-                        exceptionMessage = longMessage;
-                    } else if (!exceptionMessage.contains(longMessage)) {
-                        exceptionMessage = join(exceptionMessage, 
System.lineSeparator() + longMessage);
-                    }
+                longMessage = ((AbstractMojoExecutionException) 
t).getLongMessage();
+            } else if (t instanceof MojoException) {
+                longMessage = ((MojoException) t).getLongMessage();
+            }
+
+            if (longMessage != null && !longMessage.isEmpty()) {
+                if ((exceptionMessage == null || exceptionMessage.isEmpty())
+                        || longMessage.contains(exceptionMessage)) {
+                    exceptionMessage = longMessage;
+                } else if (!exceptionMessage.contains(longMessage)) {
+                    exceptionMessage = join(exceptionMessage, 
System.lineSeparator() + longMessage);
                 }
             }
 
diff --git 
a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
 
b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
index 5f442555bc..0c3779ae7a 100644
--- 
a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
+++ 
b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
@@ -37,6 +37,7 @@ import java.util.concurrent.locks.ReentrantLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.apache.maven.api.SessionData;
+import org.apache.maven.api.services.MavenException;
 import org.apache.maven.api.services.MessageBuilderFactory;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
@@ -324,7 +325,8 @@ public class MojoExecutor {
             } catch (MojoFailureException
                     | PluginManagerException
                     | PluginConfigurationException
-                    | MojoExecutionException e) {
+                    | MojoExecutionException
+                    | MavenException e) {
                 throw new LifecycleExecutionException(
                         messageBuilderFactory, mojoExecution, 
session.getCurrentProject(), e);
             }
diff --git 
a/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java
 
b/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java
index 84774deebc..7d3e8c4a7a 100644
--- 
a/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java
+++ 
b/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java
@@ -27,7 +27,7 @@ import java.io.PrintStream;
 import java.util.List;
 
 import org.apache.maven.api.Project;
-import org.apache.maven.api.plugin.MojoException;
+import org.apache.maven.api.services.MavenException;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.execution.MojoExecutionEvent;
 import org.apache.maven.execution.MojoExecutionListener;
@@ -143,12 +143,14 @@ public class DefaultBuildPluginManager implements 
BuildPluginManager {
                 mojoExecutionListener.beforeMojoExecution(mojoExecutionEvent);
                 mojo.execute();
                 
mojoExecutionListener.afterMojoExecutionSuccess(mojoExecutionEvent);
-            } catch (ClassCastException e) {
+            } catch (ClassCastException | MavenException e) {
                 // to be processed in the outer catch block
                 throw e;
             } catch (RuntimeException e) {
                 throw new PluginExecutionException(mojoExecution, project, e);
             }
+        } catch (MavenException e) {
+            throw e;
         } catch (PluginContainerException e) {
             mojoExecutionListener.afterExecutionFailure(
                     new MojoExecutionEvent(session, project, mojoExecution, 
mojo, e));
@@ -226,12 +228,8 @@ public class DefaultBuildPluginManager implements 
BuildPluginManager {
         }
 
         @Override
-        public void execute() throws MojoExecutionException, 
MojoFailureException {
-            try {
-                mojoV4.execute();
-            } catch (MojoException e) {
-                throw new MojoExecutionException(e.getMessage(), e);
-            }
+        public void execute() {
+            mojoV4.execute();
         }
 
         @Override

Reply via email to