From 09a81157b1be1a69e7573db705d82c2f68421f9e Mon Sep 17 00:00:00 2001 From: Stefan Oehme Date: Tue, 6 Aug 2024 15:50:15 +0200 Subject: [PATCH 1/2] 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 `MojoException` and treating this new exception type directly in `DefaultExceptionHandler` and `MojoExecutor` --- .../exception/DefaultExceptionHandler.java | 21 ++++++++++++------- .../lifecycle/internal/MojoExecutor.java | 4 +++- .../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 059e1c8e1746..90b9c93276eb 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.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 @@ private String getMessage(String message, Throwable exception) { Set 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 5f442555bc88..71892e66bd28 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.ReentrantReadWriteLock; import org.apache.maven.api.SessionData; +import org.apache.maven.api.plugin.MojoException; 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 @@ private void doExecute2(MavenSession session, MojoExecution mojoExecution) throw } catch (MojoFailureException | PluginManagerException | PluginConfigurationException - | MojoExecutionException e) { + | MojoExecutionException + | MojoException 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 84774deebc84..7d3e8c4a7ac2 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.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 void executeMojo(MavenSession session, MojoExecution mojoExecution) 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 @@ private static class MojoWrapper implements Mojo { } @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 From d98f23ff7863cfc81e07d0780ea9ee886cf34c92 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 15 Aug 2024 00:00:48 +0200 Subject: [PATCH 2/2] Broaden to MavenException --- .../org/apache/maven/lifecycle/internal/MojoExecutor.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 71892e66bd28..0c3779ae7a40 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,7 +37,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import org.apache.maven.api.SessionData; -import org.apache.maven.api.plugin.MojoException; +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; @@ -326,7 +326,7 @@ private void doExecute2(MavenSession session, MojoExecution mojoExecution) throw | PluginManagerException | PluginConfigurationException | MojoExecutionException - | MojoException e) { + | MavenException e) { throw new LifecycleExecutionException( messageBuilderFactory, mojoExecution, session.getCurrentProject(), e); }