top of page

The Power of Skipping Tests: How to Speed Up Your Maven Build Process

  • pranaypourkar
  • Apr 23, 2023
  • 2 min read

Updated: May 10, 2023


Tests are an essential part of the software development process, and they help ensure that our application code is working as expected and meets the requirements.


However, there may be some situations where we might want to skip tests temporarily, such as when build process is taking long time to complete especially if your application has a large test suite.


Note that skipping tests may be necessary in some situations, it's important to remember that tests are critical to ensuring the quality and reliability of your application. Our aim should be to have a comprehensive test suite that covers all aspects of your application code and that is run regularly as part of your development process.



Let's see how we can skip tests in Maven during build process.

To build a Maven application while skipping the tests, we can use the -DskipTests command-line option.

mvn clean install -DskipTests

If we are using a Maven plugin that runs tests, such as the maven-surefire-plugin, the -DskipTests option may not work as expected. This is because the plugin itself may ignore the -DskipTests option and still run the tests.

In such cases, we can configure the plugin to skip the tests by setting the skipTests parameter to true in the plugin configuration. Here's an example configuration for the maven-surefire-plugin:

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

This configuration sets the skipTests parameter to true, which tells the maven-surefire-plugin to skip running the tests.


Alternatively, we can also use the maven.test.skip property to skip all tests in your Maven project, including those run by plugins. To do this, we can run the following command:

mvn install -Dmaven.test.skip=true


Note that it is also possible to exclude some of the test classes from running during a Maven build by using the maven-surefire-plugin or maven-failsafe-plugin.


We can exclude specific test classes by configuring the excludes parameter in the plugin's configuration. Here's an example of how to exclude a test class using the maven-surefire-plugin.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0</version>
      <configuration>
        <excludes>
          <exclude>**/SomeTestClass1.java</exclude>
          <exclude>**/SomeTestClass2.java</exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>



Thank you for taking the time to read this post on skipping tests in your Maven project. I hope that you found it informative and useful in your own development work. By making informed decisions about when and why to skip tests, we can help ensure that our applications are of the highest quality and reliability.

Comments


bottom of page