Languages
[Edit]
EN

Maven skip tests

14 points
Created by:
kaya31
526

Most communly used command:

mvn clean install -DskipTests=true

# or
# maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin:
mvn clean install -Dmaven.test.skip=true

 

1. Overview

In this post we will see how to skip unit tests when we build our project with maven.

Easiest way to skip unit tests with maven is by using one of the commands:

mvn clean install -DskipTests=true
# or
mvn install -DskipTests=true


mvn clean install -Dmaven.test.skip=true
# or
mvn install -Dmaven.test.skip=true

or

mvn clean package -DskipTests=true
# or
mvn package -DskipTests=true


mvn clean package -Dmaven.test.skip=true
# or
mvn package -Dmaven.test.skip=true

Example of maven clean command:

# before running install or package, run clean to clear target directory
mvn clean

Sometimes we may want to build our project without running all unit tests again.

Or some of the unit tests are failing and we want to build the jar anyway.

There is more then one way to accomplish it.

Maven -D parameter means - define a system property, from mvn -help command.

Maven - skip unit tests screenshot with Console output of maven command: mvn package -Dmaven.test.skip=true - link to post: https://dirask.com/q/k1wL3D
Console output of maven command: mvn package -Dmaven.test.skip=true

2. Skip unit tests with -Dmaven.test.skip=true

We can run maven commands with defined system property:

-Dmaven.test.skip=true

Below we have examples with mvn install and mvn package.

2.1 mvn install

mvn install -Dmaven.test.skip=true
# our build will execute without unit tests

2.2 mvn package

mvn package -Dmaven.test.skip=true
# our build will execute without unit tests

NOTE: maven.test.skip is honored by:

  • Maven Surefire Plugin
  • Maven Failsafe Plugin
  • Maven Compiler Plugin

3. Disable unit tests permanently in pom.xml

Add this property to pom.xml

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

When we have this property defined in our pom.xml, we can run

mvn install
# our build will execute without unit tests

or

mvn package
# our build will execute without unit tests

Maven will accept property from pom.xml and skip running unit tests.

4. If we use Maven Surefire Plugin

Add this xml property to Maven Surefire Plugin configuration in pom.xml:

<configuration>
  <skipTests>true</skipTests>
</configuration>

Full pom.xml example, where we need to add this configuration to skip unit tests:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20</version>

        <!-- HERE it goes -->
        <configuration>
          <skipTests>true</skipTests>
        </configuration>


      </plugin>
    </plugins>
  </build>
  [...]
</project>

5. Maven -D parameter mean - explanation

What does maven -D parameter mean?
It means - define a system property.

-D, --define <arg>

To define property we need to use -D.
Synonym of '-D' parameter is '--define'

Examples:

mvn install -DskipTests=true
mvn install -DfailIfNoTests=false

When we execture mvn -help, we will get description of all flags.

$ mvn -help
..
..
-D, --define <arg>  Define a system property
..
..

6. mvn diff between clean install vs clean package

mvn clean - will remove our target directory

mvn install - will build our jar + put it into target dir and copy it to maven .m2 repository on our PC, example of maven local repository path:

C:\Users\seth\.m2\repository\com\dirask\0.0.1-SNAPSHOT\dirarsk-examples-0.0.1-SNAPSHOT.jar

mvn package - will build our jar + put it into target dir, nothing more and nothing less

From maven documentation:

  • install - install the package into the local repository, for use as a dependency in other projects locally
  • package - take the compiled code and package it in its distributable format, such as a JAR.

7. Maven clean - why to run it?

It is good practice to run maven clean command before running mvn install or mvn package command. Maven clean will remove our target directory and in that way we will ensure our code and resources are freshly compiled to target directory.

Merged questions

  1. Maven - how to skip unit tests
  2. How do I skip tests with maven?
  3. mvn skip unit tests
  4. mvn clean install skip test
  5. mvn package skip test

References

  1. Maven Surefire Plugin - skipping tests
  2. Maven Failsafe Plugin - skipping tests
  3. Maven Compiler Plugin
  4. Introduction to the Standard Directory Layout
  5. Introduction to the Build Lifecycle

Alternative titles

  1. mvn skip tests
  2. Which command is used for skipping the test in Maven?
  3. mvn clean install
  4. maven clean install
  5. mvn clean install skip tests
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Maven

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join