When to use MojoExecutionException vs MojoFailureException in Maven

Rich Seller picture Rich Seller · Jun 23, 2009 · Viewed 7k times · Source

The AbstractMavenMojo's execute method declares it throws two exceptions, MojoExecutionException and MojoFailureException. Throwing either results in the build stopping and the log displays an almost identical message in each case.

The message for MojoExecutionException is:

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] [exception text]
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------

and the message for MojoFailureException is:

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] [exception text]
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------

When should you throw which exception and why?

Answer

Kingamajick picture Kingamajick · Jul 1, 2009

It seems you should throw a MojoExecutionException if the problem makes it impossible to continue with the build, and use the MojoFailureException otherwise.

You can control the behavior for handing MojoFailureExpections when maven is run.

The following link details the difference: https://books.sonatype.com/mvnref-book/reference/writing-plugins-sect-custom-plugin.html#writing-plugins-sect-failure
Broken link? Google search sonatype writing-plugins-sect-custom-plugin writing-plugins-sect-failure