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. Also to speed up packaging, unit tests also need to be skipped.
Maven skips unit tests for five methods.
To run a Springboot application in a formal environment, we need to package it first, and then use it to java -jar xx.jar
run our project.
We usually use the development or test database in development, which is generally isolated from the production one, which means that the production configuration file needs to be activated when packaging, but we do not necessarily have access to the production database. At this time There will be problems when we pack it directly. When we directly click on the package above, it will activate the unit test, which needs to pass the test before it can be packaged, but obviously the test cannot pass, because I activated the production configuration but I do not have access to the production library. Sometimes you will fall into the feeling of always packing but not finishing, which requires us to skip the test when packing. So how to skip the test? Let's explore the solution to this problem:
1. Skip the test in the command line mode
We can use the command to package the project and add the command to skip the test. There are two commands to skip the test:
-
mvn package -DskipTests=true
-DskipTests=true
Do not execute the test case, but compile the test case class to generate the corresponding class file to target/test-classes.
-
mvn package -Dmaven.test.skip=true
-Dmaven.test.skip=true
Test cases are not executed and test case classes are not compiled.
When mvn package
compiling and packaging with , Maven will execute src/test/java
the JUnit test cases in , sometimes in order to skip the test, parameters -DskipTests=true
and will be used -Dmaven.test.skip=true
. The main difference between these two parameters is:
Use -Dmaven.test.skip=true
, not only skip the running of the unit test, but also skip the compilation of the test code;
use to -DskipTests=true
skip the unit test, but continue to compile.
2. Configure the skip test in pom.xml
You can add the following configuration in 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. Idea direct configuration
The toolbar of the Maven command bar has the icon in the figure below, and this icon is it Skip Tests
. Click to select, and then use packaging in LifeStyle to skip the test. Note: Because my IDEA is the 2022 version, the icon may be slightly different from the previous version. The previous version should be a blue circle with a lightning bolt inside.
4. Add Maven configuration parameters
Open the configuration, find Build, Exxcution, Deployment –> Maven Tools –> Maven –> Runner , add -Dmaven.test.skip=true
or -DskipTests=true
, you can skip the test when packaging.
5. By changing the settings
Open the configuration, find Build, Exxcution, Deployment –> Maven Tools –> Maven –> Runner , and check the Skip Test option in Properties.