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?
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With