How to Skip Tests When Packaging a Maven Project
This article explains why Maven runs unit tests during the package phase, describes the problems caused by missing production database access, and provides five practical methods—including command‑line flags, pom.xml configuration, IDE settings, and Maven runner options—to skip tests when building a Java project.
When building a Java project with Maven, the default lifecycle runs unit tests before the package phase, which can fail if the production database is inaccessible. Skipping these tests allows the project to be packaged into a JAR file and run with java -jar xx.jar .
1. Command‑line way to skip tests
Use one of the following commands:
mvn package -DskipTests=true-DskipTests=true : skips test execution but still compiles test classes.
mvn package -Dmaven.test.skip=true-Dmaven.test.skip=true : skips both test execution and test compilation.
2. Configure pom.xml to skip tests
<build>
<plugins>
<!-- Maven skip tests during packaging -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>3. IDE button to skip tests
In IntelliJ IDEA, click the Skip Tests icon in the Maven tool window and then run the LifeStyle packaging goal; the build will skip tests.
4. Add Maven runner VM options
Open Build, Execution, Deployment → Maven → Runner and add either -Dmaven.test.skip=true or -DskipTests=true to the VM options.
5. Enable "Skip Test" property
In the same Maven Runner settings, check the Skip Test property to bypass tests during packaging.
These five approaches allow you to package a Maven project without running unit tests, which is useful when test execution is blocked by unavailable resources.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.