Multiple ways to skip test case execution in maven project with example

This tutorial outlines various methods to disable the execution of test cases in Maven projects.

When Maven projects attempt to package a JAR or WAR file, they automatically compile and execute test classes by default.

maven package

It compiles both Java and test source files, executes the test files, and packages the source code according to the configured module.

Maven skip test case execution

There are several ways to disable test case execution:

  • Using Command Line: Maven provides the following JVM command line arguments for the mvn command.

    • -Dmaven.test.skip.exec=true: This compiles test Java files, but does not execute test cases.
    • -Dmaven.test.skip=true: This disables both compilation and test case execution and does not generate test-jar artifacts in the target folder. -DskipTests is a shorthand argument equivalent to -Dmaven.test.skip=true.

Here is an example of using the package goal in a Maven command:

mvn package -Dmaven.test.skip=true
or
mvn package -DskipTests

This command will start the packaging process for the Maven project, which includes compiling source code, executing tests, and creating the specified package (JAR or WAR).

For mvn clean install command

mvn clean install -Dmaven.test.skip=true
or
mvn clean install -DskipTests
  • using the pom.xml plugin To disable test case execution using the maven-surefire-plugin, you can set the skipTests property to true in the plugin configuration.

Here’s an example:

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

By settingtrue, you instruct the maven-surefire-plugin to skip the execution of tests during the build process.

  • pom.xml properties Indeed, you can add the maven.test.skip property to the properties section of the pom.xml file to control test case execution at the project or module level. Here’s an example:
<properties>
    <maven.test.skip>true</maven.test.skip>
</properties>

By setting <maven.test.skip>true</maven.test.skip>, you globally skip the execution of tests for the entire project or module. This provides a convenient way to manage test execution configuration at a broader level within the Maven project.

Conclusion

Choose the method that best fits your requirements, considering whether you want to skip tests globally or selectively at different levels within your Maven project.