-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[MNG-8541] Support throwing Exception from Mojo#execute #2066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,28 @@ | |
| import org.apache.maven.api.annotations.ThreadSafe; | ||
|
|
||
| /** | ||
| * This interface forms the contract required for Mojos to interact with the Maven infrastructure. | ||
| * It features an {@link #execute()} method, which triggers the Mojo's build-process behavior, | ||
| * and can throw a {@link MojoException} if error conditions occur. | ||
| * Represents the contract for Mojos to interact with the Maven infrastructure. | ||
| * Implementations of this interface define specific build-process behaviors | ||
| * that are triggered during a Maven build lifecycle. | ||
| * | ||
| * The primary entry point is the {@link #execute()} method, which encapsulates | ||
| * the behavior of the Mojo and serves as the integration point with Maven. This | ||
| * method may throw an {@link Exception} to signal any issues that prevent | ||
| * successful execution of the Mojo. | ||
| * | ||
| * <p> | ||
| * Annotations: | ||
| * </p> | ||
| * <ul> | ||
| * <li>{@link Experimental}: Indicates that this interface or its implementation | ||
| * may still be evolving and could change in future versions.</li> | ||
| * <li>{@link FunctionalInterface}: Denotes that this is a functional interface, | ||
| * allowing implementations as lambda expressions or method references.</li> | ||
| * <li>{@link Consumer}: Signifies that this type is intended to be implemented | ||
| * or extended by Maven plugins or extensions and consumed by Maven itself.</li> | ||
| * <li>{@link ThreadSafe}: Implies that implementations of this interface must | ||
| * be safe to invoke from multiple threads concurrently.</li> | ||
| * </ul> | ||
| * | ||
| * @since 4.0.0 | ||
| */ | ||
|
|
@@ -35,11 +54,12 @@ | |
| @ThreadSafe | ||
| public interface Mojo { | ||
| /** | ||
| * Perform whatever build-process behavior this {@code Mojo} implements. | ||
| * This is the main trigger for the {@code Mojo} inside the Maven system, | ||
| * and allows the {@code Mojo} to communicate errors. | ||
| * Perform whatever build-process behavior this {@code Mojo} implements. This is | ||
| * the main trigger for the {@code Mojo} inside the Maven system, and allows the | ||
| * {@code Mojo} to communicate errors. | ||
| * | ||
| * @throws MojoException if a problem occurs | ||
| * @throws Exception if any problem occurs that prevents the mojo from its | ||
| * execution | ||
|
||
| */ | ||
| void execute(); | ||
| void execute() throws Exception; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,7 @@ public DefaultBuildPluginManager( | |
| * @throws PluginResolutionException The plugin could be found but could not be resolved. | ||
| * @throws InvalidPluginDescriptorException | ||
| */ | ||
| @Override | ||
| public PluginDescriptor loadPlugin( | ||
| Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) | ||
| throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, | ||
|
|
@@ -92,6 +93,7 @@ public PluginDescriptor loadPlugin( | |
| // Mojo execution | ||
| // ---------------------------------------------------------------------- | ||
|
|
||
| @Override | ||
| public void executeMojo(MavenSession session, MojoExecution mojoExecution) | ||
| throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException { | ||
| MavenProject project = session.getCurrentProject(); | ||
|
|
@@ -201,6 +203,7 @@ public void executeMojo(MavenSession session, MojoExecution mojoExecution) | |
| * call, which is not nice. | ||
| * @throws PluginResolutionException | ||
| */ | ||
| @Override | ||
| public ClassRealm getPluginRealm(MavenSession session, PluginDescriptor pluginDescriptor) | ||
| throws PluginResolutionException, PluginManagerException { | ||
| ClassRealm pluginRealm = pluginDescriptor.getClassRealm(); | ||
|
|
@@ -213,6 +216,7 @@ public ClassRealm getPluginRealm(MavenSession session, PluginDescriptor pluginDe | |
| return pluginDescriptor.getClassRealm(); | ||
| } | ||
|
|
||
| @Override | ||
| public MojoDescriptor getMojoDescriptor( | ||
| Plugin plugin, String goal, List<RemoteRepository> repositories, RepositorySystemSession session) | ||
| throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, | ||
|
|
@@ -228,8 +232,14 @@ private static class MojoWrapper implements Mojo { | |
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| mojoV4.execute(); | ||
| public void execute() throws org.apache.maven.plugin.MojoExecutionException { | ||
|
||
| try { | ||
| mojoV4.execute(); | ||
| } catch (RuntimeException e) { | ||
| throw e; | ||
| } catch (Exception e) { | ||
| throw new MojoExecutionException(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a small incoherence between the javadoc on the class which talks about MojoException, which is not the only exception thrown anymore.
Going through AI, It came up with:
which looks better to me. Wdyt ?