Alex
Alex

Reputation: 670

How to compile all maven modules even if tests fail, but fail overall build if any tests fail

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.

Upvotes: 22

Views: 9089

Answers (4)

Maxim Kochetkov
Maxim Kochetkov

Reputation: 76

If you are using surefire plugin and test reports are enabled

My approach in CI. After build I check .txt report files that indicate FAILED tests:

mvn clean install -DfailIfNoTests=false --fail-never --no-transfer-progress

grep -lq "FAILURE!" ./*/target/surefire-reports/*.txt && echo "--- FAILED ---" && exit 1

echo "--- SUCCESS ---"

Upvotes: 1

Bruno Bieth
Bruno Bieth

Reputation: 2387

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

Upvotes: 3

sradi
sradi

Reputation: 71

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.

Upvotes: 3

khmarbaise
khmarbaise

Reputation: 97447

I would suggest to use:

mvn -Dmaven.test.failure.ignore=true --fail-at-end clean install

Upvotes: 6

Related Questions