When using Maven to package, it may fail to package because of the unit test, and you need to skip the unit test at this time.
Maven skipping unit tests can be mainly divided into three methods.
1. Skip the test in the command line mode
We can skip tests with two commands
- mvn clean install -DskipTests
- mvn clean install -Dmaven.test.skip=true
- DskipTests, do not execute test cases, but compile test case classes to generate corresponding class files to target/test-classes
- Dmaven.test.skip=true, do not execute test cases and do not compile test case classes
Using maven.test.skip not only skips the running of unit tests, but also skips the compilation of test codes;
Use mvn package -DskipTests to skip unit tests, but continue compiling.
2. Configure the skip test in pom.xml
You can add the following configuration to pom.xml to skip tests:
<build>
<plugins>
<!-- maven 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<build>
3. Configure Skip Test in Idea
Idea is our commonly used development tool, and we often directly use Idea plug-ins for packaging. We can skip tests by configuration.
1. Direct configuration
The toolbar of the Maven command bar has the icon in the figure below, and this icon is Skip Tests. Click to select, and then use the package in LifeStyle to skip the test.
2. Change the Maven configuration
- Method 1: Open the configuration, find Maven-->Runner, and add it in the VM option-Dmaven.test.skip=true
- Method 2: Select in Runner--> PropertiesSkip tests
reference:
[1]: Maven skips compiling test in two ways
[2]: How to skip the test when Maven is packaged in IDEA
[3]: Two methods to skip the test when mvn packages the project
[4]: Configure Maven to skip in Idea Test compile operation