Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Introduction

This quick guide shows how to build a jar with Maven while ignoring the test results.

By default, Maven runs unit tests automatically while building the project. However, there are rare cases when the tests can be skipped and we need to build the project regardless of the test results.

2. Building the Project

Let's create a simple project where we also include a small test case:

public class TestFail {
    public void whenMessageAssigned_thenItIsNotNull() {
        String message = "hello there";

Let's build a jar file by executing the following Maven command:

mvn package

This will result in compiling the sources and generate a maven-0.0.1-SNAPSHOT.jar file under the /target directory.

Now, let's change the test a bit, so the test starts to fail.

public void whenMessageAssigned_thenItIsNotNull() {
    String message = null;

This time, when we try to run the mvn package command again, the build fails and the maven-0.0.1-SNAPSHOT.jar file isn't created.

This means, if we have a failing test in our application, we can't provide an executable file unless we fix the test.

So how can we solve this problem?

3. Maven Arguments

Maven has its own arguments to deal with this issue:

  • -Dmaven.test.failure.ignore=true ignores any failure that occurs during test execution
  • -Dmaven.test.skip=true would not compile the tests
  • -fn, -fae never fails the build regardless of test results

Let's run the mvn package -Dmaven.test.skip=true command and see the results:

[INFO] Tests are skipped.

This means the project will be built without compiling the tests.

Now let's run the mvn package -Dmaven.test.failure.ignore=true command:

[INFO] Running testfail.TestFail
[ERROR] whenMessageAssigned_thenItIsNotNull java.lang.AssertionError

Our unit test fails on assertion but the build is successful.

Finally, let's test the -fn, -fae options. Both, package -fn and package -fae commands build the jar file and produce the BUILD SUCCESS output regardless of whenMessageAssigned_thenItIsNotNull() test fail.

In case of the multi-module project -fn option should be used. -fae continues with the module that has a failing test but skips all the dependent modules. 

4. Maven Surefire Plugin

Another convenient way to achieve our goal is to use Maven's Surefire plugin.

For an extended overview of the Surefire plugin, refer to this article.

To ignore test fails we can simply set the testFailureIgnore property to true:


Now, let's see the output of the package command:

[INFO] Running testfail.TestFail
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, <<< FAILURE! - in testfail.TestFail

From the running tests output, we can see the TestFail class is failing. But looking further we see that the BUILD SUCCESS message is also there and the maven-0.0.1-SNAPSHOT.jar file is compiled.

Depending on our need, we can skip running the tests at all. For that we can replace the testFailureIgnore line with:


Or set the command line argument -DskipTests. This will compile the test classes but skip test execution entirely.

5. Conclusion

In this article, we learned how to build our project with Maven regardless of the test results. We went through the practical examples of skipping the failing tests or excluding compilation of the tests entirely.

As usual, the complete code for this article is available over on GitHub project.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Maven footer banner
Comments are closed on this article!