When to use MojoExecutionException vs MojoFailureException in Maven
Throw a MavenFailureException
if your build step failed but it could be ignored (for instance you may want to ignore failing tests).
Throw a MavenExecutionException
if there is no way to continue - say you detected an unrecoverable condition like you were trying to compile and the project could not be compiled thus everything after that would be fruitless anyways.
MavenExecutionException
will always kill the build whereas the behavior of the MavenFailureException
is configurable.
The default behavior is to fail fast, i.e. the same behavior as the build error, which is most often what you want. You can alter the behavior by passing a command line flag:
mvn -fae
fail at the end, i.e. every build step will be executed and you may experiencing subsequent failures if a stage fails, the whole build is only failed once every step has been executed.
mvn -fn
do not fail. This is useful if you want to ignore for example a failing integration test but nevertheless mvn deploy
.
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