Skip to content

Commit

Permalink
[MNG-8196] Make exception messages match Maven 3 again (#1628)
Browse files Browse the repository at this point in the history
* 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 <gnodet@gmail.com>
  • Loading branch information
oehme and gnodet authored Aug 16, 2024
1 parent 008d0b4 commit e417e45
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -232,16 +233,20 @@ private String getMessage(String message, Throwable exception) {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand Down Expand Up @@ -324,7 +325,8 @@ private void doExecute2(MavenSession session, MojoExecution mojoExecution) throw
} catch (MojoFailureException
| PluginManagerException
| PluginConfigurationException
| MojoExecutionException e) {
| MojoExecutionException
| MavenException e) {
throw new LifecycleExecutionException(
messageBuilderFactory, mojoExecution, session.getCurrentProject(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e417e45

Please sign in to comment.