How to Skip Tests When Packaging a Maven Project

This article explains why Maven runs unit tests during the package phase, describes the Maven lifecycle, and provides several practical methods—including command‑line flags, pom.xml configuration, and IDE settings—to skip tests when building a Java project.

Top Architect
Top Architect
Top Architect
How to Skip Tests When Packaging a Maven Project

Hello, I am a senior architect. In everyday development we mainly write code, and packaging a project is not difficult.

The simplest way to package is to use the Maven plugin, which creates a JAR that can be run with java -jar xx.jar.

When the production database is inaccessible, the default Maven test phase blocks packaging because tests cannot run; therefore we need to skip tests.

Maven’s default lifecycle has 23 phases, and the package phase depends on the preceding test phase, which must succeed before packaging occurs.

1. Command‑line way to skip tests

You can add skip flags to the Maven command:

mvn package -DskipTests=true
-DskipTests=true

skips test execution but still compiles test classes into target/test-classes.

mvn package -Dmaven.test.skip=true
-Dmaven.test.skip=true

skips both test execution and compilation of test classes.

During mvn package, Maven runs tests in src/test/java using JUnit; the two flags control whether tests are executed or compiled.

2. Configure pom.xml to skip tests

Add the following plugin configuration to your pom.xml:

<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. Direct IDE configuration

In the Maven tool window, click the “Skip Tests” icon and then run the “Package” lifecycle; the build will skip tests.

4. Add Maven parameters in IDE settings

Open

Build → Execution → Deployment → Maven Tools → Maven → Runner

and add -Dmaven.test.skip=true or -DskipTests=true to the VM options to skip tests during packaging.

5. Change settings to enable “Skip Test”

In the same Maven Runner properties, check the “Skip Test” option.

These are the main ways to skip tests when packaging a Maven project.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javamavenpackagingbuildCISkipTests
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.