How to build a jar using maven, ignoring test results?

106,217

Solution 1

Please refer to surefire:test for details, but the most useful properties are:

-Dmaven.test.failure.ignore=true (or -DtestFailureIgnore=true) - will ignore any failures occurred during test execution

-Dmaven.test.error.ignore=true ( deprecated ) - will ignore any errors occurred during test execution

-DskipTests - would compile the test classes but skip test execution entirely

-Dmaven.test.skip=true - would not even compile the tests

I believe that in your case where you want to compile test classes but not fail the build due to any tests errors and still create the jar.

You should use the first option to ignore any test failures which you can still review once the build has finished.

Solution 2

mvn -Dmaven.test.skip=true package skips the surefire test mojo.

to ignore test failures and keep maven from stopping you can add this to the section of the pom.xml:

 <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <configuration>
     <testFailureIgnore>true</testFailureIgnore>
   </configuration>
 </plugin>

Solution 3

The solution is:

mvn -fn clean install

execute mvn --help for advanced options

Here's the excerpt for -fn

 -fn,--fail-never         NEVER fail the build, regardless
                          of project result

Solution 4

<properties>
<maven.test.skip>true</maven.test.skip>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
</properties>

http://jira.codehaus.org/browse/SUREFIRE-319

Or from command line

http://maven.apache.org/maven-1.x/plugins/test/properties.html

maven.test.error.ignore Yes Set this to true to ignore errors during testing. Its use is NOT RECOMMENDED, but quite convenient on occasion

Solution 5

Use -DskipTests=true instead of -Dmaven.test.skip=true in order to skip tests but compile them.

Using -Dmaven.test.failure.ignore=true will also work but is not very nice.

Share:
106,217
user398920
Author by

user398920

Updated on August 10, 2020

Comments

  • user398920
    user398920 over 3 years

    Actuality when i run tests they fails but i need to run them to get some .class files which are very important for my jar.

    By default when test results fails , the jar is not build , could i add a setting in pom.xml which ignore that, so I can build the jar ignoring results from tests ?

    I read something about "Maven Surefire Plugin" but I don't know how to use it...

  • user398920
    user398920 almost 14 years
    Thanks a lot for your post, i added that part and it worked , any way, my problem was not solved because , the .class files which result after running tests now are missing... and i need those... somehow... even if test results fails
  • user398920
    user398920 almost 14 years
    Thanks a lot for your post, i added that part and it worked , any way, my problem was not solved because , the .class files which result after running tests now are missing... and i need those... somehow... even if test results fails
  • user398920
    user398920 almost 14 years
    Thanks a lot for your post, i added that part and it worked , any way, my problem was not solved because , the .class files which result after running tests now are missing... and i need those... somehow... even if test results fails
  • user398920
    user398920 almost 14 years
    sorry i was wrong, removing "<maven.test.skip>true</maven.test.skip>" part i get the right results
  • fasseg
    fasseg almost 14 years
    i added a new part to the answer regarding surefire plugin config for ignoring test failures...hope that helps..
  • Devanshu Mevada
    Devanshu Mevada almost 14 years
    This will skip the tests, the question is more about ignoring failures.
  • Sean Patrick Floyd
    Sean Patrick Floyd almost 14 years
    mvn -Dmaven.test.skip=true package skips the testing phase. No it doesn't!. You can't skip a phase by using a system property. What it does is set a flag that skips execution of the surefire:test mojo and the compiler:test-compile mojo (possibly others) : maven.apache.org/plugins/maven-surefire-plugin/… maven.apache.org/plugins/maven-compiler-plugin/…
  • Devanshu Mevada
    Devanshu Mevada almost 14 years
    That's not nitpicking, that's correctness. And all these answers suggesting to skip tests are incorrect anyway, they missed the point: the question is not about skipping tests, it's about ignoring tests failures.
  • rogerdpack
    rogerdpack over 12 years
    for me it appears that -Dmaven.test.failure.ignore=true works from the command line but -Dmaven.test.error.ignore=true does nothing.
  • Blaisorblade
    Blaisorblade almost 12 years
    @rogerdpack: failures and errors are different in some testing frameworks; from your message, I guess you were getting failures and not errors.
  • Zilvinas
    Zilvinas almost 11 years
    I would not recommend setting this property in the pom.xml, but rather only run it when really required by adding this directly from command line
  • Zilvinas
    Zilvinas almost 11 years
    this is not the best solution, as it will stop building the project that failed, meaning that whatever goals where set to be done after test execution ( package, install, deploy & etc ) will not be executed for that project. So the "full build" will run till the end, but that project will not be installed to repo and the previous version of the jar will be used where it is defined as a dependency
  • Sean Patrick Floyd
    Sean Patrick Floyd almost 11 years
    @Zilvinas so what is the best solution?
  • Zilvinas
    Zilvinas over 10 years
    see the answer below: stackoverflow.com/a/16690564/344477
  • PAULUS
    PAULUS over 10 years
    Anyone know why "-Dmaven.test.error.ignore=true" is not being used anymore? I finding the need to not ignore actual failures, but ignore errors (which may not actually cause the test to "fail") in some cases. Is there any way to distinguish the two now?
  • Zilvinas
    Zilvinas almost 10 years
    You could use an older maven surefire plugin version to achieve this
  • divideByZero
    divideByZero over 8 years
    Build fails when I use -DtestFailureIgnore=true, whereas -DtestFailureIgnore=true works. I use maven-3.2.1
  • Zilvinas
    Zilvinas over 8 years
    @divideByZero too much copy paste, you just wrote the same thing twice. So which way does it (not) work?
  • Kingsly
    Kingsly over 8 years
    @Zilvinas maven.test.failure.ignore worked, but testFailureIgnore did not work for me with Maven-2.2.1
  • Zilvinas
    Zilvinas over 8 years
    @Kingsly as specified in maven.apache.org/surefire/maven-surefire-plugin/test-mojo.ht‌​ml the testFailureIgnore is a user property, I believe you could set it inside your pom.xml. In general, they both should do the same thing. And it is not your maven version that matters, but the plugin version
  • PenguinEngineer
    PenguinEngineer about 7 years
    <maven.test.failure.ignore>true</maven.test.failure.ignore> works for me, while <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> not. Thanks
  • Shiva
    Shiva almost 7 years
    If test-jar type, generated from test Class is used as dependency in other module whole compilation will fail if we use '-Dmaven.test.skip=true'. While -DskipTests=true compiles and generates jar but doesn't run test. :)