Maven Command to Skip Test

In this quick guide, we will see how to skip running JUnit tests in Maven projects.

Why Skip Tests? 

Speeding up the build: Sometimes, especially during development, you might want to deploy quickly without waiting for tests. 

Initial Setup: When setting up a project, you might want to see if everything builds correctly before worrying about tests. 

Unrelated Changes: For minor changes that don't affect the core logic, running tests might be unnecessary. 

However, remember that it's generally not a good practice to skip tests, especially before merging changes into a main or release branch.

Skipping Tests with Maven 

To skip tests during a Maven build, you can use the -DskipTests command-line option:

mvn clean install -DskipTests

When you use -DskipTests, Maven still compiles the tests but doesn't run them.

Completely Bypassing Test Compilation and Execution 

If you want to speed up the build process further by not even compiling the tests, you can use:

mvn clean install -Dmaven.test.skip=true

This command will completely bypass the test compilation and execution phase.

You can configure the below property in the pom.xml file to skip the compilation:

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

You can configure the below Maven plugin in the pom.xml file to skip the execution:

<properties>
    <tests.skip>true</tests.skip>
</properties>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <skipTests>${tests.skip}</skipTests>
    </configuration>
</plugin>

Example Scenario 

Imagine you're working on a project and have just added some comments or modified documentation. These changes won't affect the functionality of the application, so running a complete test might seem redundant. In this scenario: 

1. Navigate to your project directory. 

2. Run the command:

mvn clean install -DskipTests

3. Maven will start the build process, and compile the main and test sources, but will skip the test execution. Once complete, you'll notice the build is faster than a regular build with tests.

Comments