Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use MojoExecutionException vs MojoFailureException in Maven

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?

like image 959
Rich Seller Avatar asked Jun 23 '09 15:06

Rich Seller


2 Answers

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

like image 67
Kingamajick Avatar answered Nov 19 '22 10:11

Kingamajick


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.

like image 35
scravy Avatar answered Nov 19 '22 08:11

scravy