Skip to content

Commit d313a86

Browse files
author
Christoph Läubrich
committed
MNG-8541 - Support throwing Exception from Mojo#execute
1 parent 63e6881 commit d313a86

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/plugin/Mojo.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,28 @@
2323
import org.apache.maven.api.annotations.ThreadSafe;
2424

2525
/**
26-
* This interface forms the contract required for Mojos to interact with the Maven infrastructure.
27-
* It features an {@link #execute()} method, which triggers the Mojo's build-process behavior,
28-
* and can throw a {@link MojoException} if error conditions occur.
26+
* Represents the contract for Mojos to interact with the Maven infrastructure.
27+
* Implementations of this interface define specific build-process behaviors
28+
* that are triggered during a Maven build lifecycle.
29+
*
30+
* The primary entry point is the {@link #execute()} method, which encapsulates
31+
* the behavior of the Mojo and serves as the integration point with Maven. This
32+
* method may throw an {@link Exception} to signal any issues that prevent
33+
* successful execution of the Mojo.
34+
*
35+
* <p>
36+
* Annotations:
37+
* </p>
38+
* <ul>
39+
* <li>{@link Experimental}: Indicates that this interface or its implementation
40+
* may still be evolving and could change in future versions.</li>
41+
* <li>{@link FunctionalInterface}: Denotes that this is a functional interface,
42+
* allowing implementations as lambda expressions or method references.</li>
43+
* <li>{@link Consumer}: Signifies that this type is intended to be implemented
44+
* or extended by Maven plugins or extensions and consumed by Maven itself.</li>
45+
* <li>{@link ThreadSafe}: Implies that implementations of this interface must
46+
* be safe to invoke from multiple threads concurrently.</li>
47+
* </ul>
2948
*
3049
* @since 4.0.0
3150
*/
@@ -35,11 +54,12 @@
3554
@ThreadSafe
3655
public interface Mojo {
3756
/**
38-
* Perform whatever build-process behavior this {@code Mojo} implements.
39-
* This is the main trigger for the {@code Mojo} inside the Maven system,
40-
* and allows the {@code Mojo} to communicate errors.
57+
* Perform whatever build-process behavior this {@code Mojo} implements. This is
58+
* the main trigger for the {@code Mojo} inside the Maven system, and allows the
59+
* {@code Mojo} to communicate errors.
4160
*
42-
* @throws MojoException if a problem occurs
61+
* @throws Exception if any problem occurs that prevents the mojo from its
62+
* execution
4363
*/
44-
void execute();
64+
void execute() throws Exception;
4565
}

impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public DefaultBuildPluginManager(
8181
* @throws PluginResolutionException The plugin could be found but could not be resolved.
8282
* @throws InvalidPluginDescriptorException
8383
*/
84+
@Override
8485
public PluginDescriptor loadPlugin(
8586
Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session)
8687
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
@@ -92,6 +93,7 @@ public PluginDescriptor loadPlugin(
9293
// Mojo execution
9394
// ----------------------------------------------------------------------
9495

96+
@Override
9597
public void executeMojo(MavenSession session, MojoExecution mojoExecution)
9698
throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException {
9799
MavenProject project = session.getCurrentProject();
@@ -201,6 +203,7 @@ public void executeMojo(MavenSession session, MojoExecution mojoExecution)
201203
* call, which is not nice.
202204
* @throws PluginResolutionException
203205
*/
206+
@Override
204207
public ClassRealm getPluginRealm(MavenSession session, PluginDescriptor pluginDescriptor)
205208
throws PluginResolutionException, PluginManagerException {
206209
ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
@@ -213,6 +216,7 @@ public ClassRealm getPluginRealm(MavenSession session, PluginDescriptor pluginDe
213216
return pluginDescriptor.getClassRealm();
214217
}
215218

219+
@Override
216220
public MojoDescriptor getMojoDescriptor(
217221
Plugin plugin, String goal, List<RemoteRepository> repositories, RepositorySystemSession session)
218222
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
@@ -228,8 +232,14 @@ private static class MojoWrapper implements Mojo {
228232
}
229233

230234
@Override
231-
public void execute() {
232-
mojoV4.execute();
235+
public void execute() throws org.apache.maven.plugin.MojoExecutionException {
236+
try {
237+
mojoV4.execute();
238+
} catch (RuntimeException e) {
239+
throw e;
240+
} catch (Exception e) {
241+
throw new MojoExecutionException(e);
242+
}
233243
}
234244

235245
@Override

0 commit comments

Comments
 (0)