22

Context: I want to compile and test all modules in a multi-module project but if any fail either compilation or tests I want the overall build to fail.

Default configurations either stop on the first failure or skip modules after a test failure

Running:

mvn clean install

stops at the first failing module.

If you add:

mvn clean install -fae //fail at end

then all modules are run, but if tests fail then any dependent modules are skpped:



    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Summary:
    [INFO] ------------------------------------------------------------------------
    [INFO] Module A ............................................. SUCCESS [15.210s]
    [INFO] Module B ............................................. SUCCESS [10.923s]
    [INFO] Module C ............................................. FAILED [1.731s]
    [INFO] Module D ............................................. SUCCESS [3.791s]
    [INFO] Module E ............................................. SUCCESS [1.488s]
    [INFO] Module F ............................................. SKIPPED (dependency build failed or was skipped)
    [INFO] Module G ............................................. SKIPPED (dependency build failed or was skipped)
    [INFO] Module H ............................................. SKIPPED (dependency build failed or was skipped)
    [INFO] Module I ............................................. SUCCESS [1.690s]
    [INFO] -----------------------------------------

Another option to force all modules to compile is:

mvn clean install -fn //fail never

but this results in the build passing when tests fail



    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Summary:
    [INFO] ------------------------------------------------------------------------
    [INFO] Module A ............................................. SUCCESS [15.210s]
    [INFO] Module B ............................................. SUCCESS [10.923s]
    [INFO] Module C ............................................. FAILED [1.731s]
    [INFO] Module D ............................................. SUCCESS [3.791s]
    [INFO] Module E ............................................. SUCCESS [1.488s]
    [INFO] Module F ............................................. SUCCESS [9.062s]
    [INFO] Module G ............................................. SUCCESS [16.324s]
    [INFO] Module H ............................................. SUCCESS [4.032s]
    [INFO] Module I ............................................. SUCCESS [1.690s]
    [INFO] ------------------------------------------------------------------------
    [INFO] Error for project: Module C (during install)
    [INFO] ------------------------------------------------------------------------
    [INFO] There are test failures.

    Please refer to C:\MavenBuildDir\ModuleC\surefire-reports for the
    individual test results.
    [INFO] ------------------------------------------------------------------------
    [INFO] For more information, run Maven with the -e switch
    [INFO] ------------------------------------------------------------------------
    [INFO]  + Ignoring failures
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 30 minutes 38 seconds
    [INFO] Finished at: Fri May 23 16:42:08 BST 2014
    [INFO] Final Memory: 39M/185M

Can anyone advise a set of options to achieve the following:

  1. compile all modules
  2. run tests on all modules
  3. If a module's tests fail but the code compiles dependent modules still get compiled and tested

Responses much appreciated - otherwise we have to run the tests repeatedly on the build server if there are multiple issues - burning a lot of time.

Alex
  • 670
  • 11
  • 21

3 Answers3

6

I would suggest to use:

mvn -Dmaven.test.failure.ignore=true --fail-at-end clean install
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Thanks for your reply - I'll give this a shot. Will accept once I've tested these settings. – Alex May 26 '14 at 13:20
  • 2
    I've tried this but unfortunately it still skips the modules. On option might be a mvn clean install -fn //fail never followed by a mvn clean install so the build fails. but nasty as requires another run. I'll leave the question unanswered for now and may post this with Sonatype. – Alex May 27 '14 at 15:48
  • 1
    This seems to do the same thing as running with `--fail-never`? – Vivek Chavda Feb 20 '17 at 21:25
  • This results in the should-be-failing modules reporting success, rather than only allowing the modules that depend on them to run their tests. – Matthew Read Feb 19 '21 at 17:06
3

I would suggest to split it into two mvn calls:

mvn clean compile
mvn -fae install

The first call will fail, if there are compile errors. The second call will reuse the compiled .class-files, since "clean" is omitted. It will fail at the end, if there are test failures. But compilation has already been finished for ALL modules.

sradi
  • 71
  • 1
  • 5
  • Thanks for the reply. khmarbaise's suggestion avoid the need to compile twice also I'd expect the first call to fail at the first error and the second to then skip over subsequent errors. However the implied efficiency of compile once means I'll prefer this answer if it works. – Alex May 26 '14 at 13:23
  • 2
    This solution will still skip dependent modules once tests fail - which is the aim of the question - so this won't work. I've considered doing mvn clean install -fae -Dmaven.test.skip=true to build all modules without tests then mvn test -fae to run all the tests. But don't like two maven calls. – Alex May 27 '14 at 15:51
  • `install` will re-run `compile`, since it is a previous step in [the relevant lifecycle](http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#default-lifecycle) and all previous steps are re-run. `clean` does additional cleanup work beyond removing compiled classfiles. – Matthew Read Nov 18 '21 at 20:01
3

Here is a different approach: parse maven output. So either you

Community
  • 1
  • 1
Bruno Bieth
  • 2,317
  • 20
  • 31