How to build with Maven without running tests

Do you try to build your app after the slightest change in code, and it takes ages to pass all the tests? What if there would be a workaround to obey all unnecessary tests and shorten the Maven build time significantly. And all that just but simple maven flag.

Either you want to build you SpringBoot app into the executable JAR file or build classes for a package, remember to use single Maven flag parameter mvn install -DskipTests and in the most of the cases you will be fine.

However, the rabbit might run deeper, so it will be better to write a bit more info.

-DskipTests option only works from Surefire 2.4. Unfortunately, there are no error messages if you do try to use it with Surefire 2.3 or lower. Therefore, your first task is to check your version of Surefire. For this, run mvn -X test and look for a line that mentions surefire. It should look like this: maven-surefire-plugin: resolved to version 2.3 from repository central. If you use pre-Surefire-2.4 start using -Dmaven.test.skip=true.

Fortunately beyond 2020 older version usage shrinks and you should remember to always favor a -DskipTests above -Dmaven.test.skip=true.

Surefire itself is the intermediate plugin between Maven and JUnit/TestNG for which while -DskipTests works and -Dmaven.test.skip=true works for compiler.

You can find (not really much more) additional info about shortening the build time trough skipping the project tests at maven skipping tests site.

You can add this plugin configuration to your pom if you do not want to set command line arguments:

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

Or you can also set skipping tests in you POM file over property:

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

Bonus You can also skip tests in IntelliJ Idea. Just go to Maven tool tab and hit ‘Skip Test’ mode.
Skip test Maven in IntelliJ Idea

This entry was posted in Tips & tricks and tagged , , , , , , , , , , . Bookmark the permalink.

1 Response to How to build with Maven without running tests

  1. Pingback: How to remotely debug Java application | Codepills.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.