Question
How can I run a Gradle build without executing unit tests?
I tried the following command, but it did not skip the tests:
gradle -Dskip.tests build
Is there a Gradle command that builds the project while excluding the unit test task?
Short Answer
You will learn how Gradle tasks and task dependencies work, how to exclude the standard test task from a build, and why adding an arbitrary -D property does not automatically change Gradle's behavior.
Concept
Gradle builds are made of tasks. A task is a named unit of work, such as compiling source code, running tests, creating a JAR, or publishing an artifact.
For many Java projects, the build task depends on a lifecycle such as:
build → check → test
build → assemble → jar
Therefore, running:
./gradlew build
normally runs compilation, tests, checks, and packaging.
To run a build while leaving out a task, use Gradle's task exclusion option:
./gradlew build -x test
Here, -x means exclude. Gradle constructs the requested task graph for build, then removes the test task from that graph.
The command -Dskip.tests does not work by itself because -D sets a Java system property. Gradle does not have a built-in system property named skip.tests that automatically disables tests. A property only has an effect when a build script, plugin, or application explicitly reads and uses it.
Mental Model
Think of a Gradle build as a checklist for preparing a product:
compileJavaprepares the parts.testperforms quality checks.jarpackages the product.buildasks Gradle to complete the whole checklist.
Using -x test is like telling the checklist manager: “Complete the build, but cross out the unit-test inspection step.”
Using -Dskip.tests is like writing a note called skip.tests and putting it on the desk. Nothing changes unless someone has written instructions that say to read that note and act on it.
Syntax and Examples
Use -x followed by the exact Gradle task name to exclude a task.
./gradlew build -x test
On systems where the Gradle wrapper is not used, the equivalent is:
gradle build -x test
./gradlew is usually preferred because the Gradle Wrapper uses the project’s configured Gradle version.
Excluding more than one task
You can repeat -x when your project has additional test tasks:
./gradlew build -x test -x integrationTest
This only works if integrationTest is actually a task in the project.
Seeing available tasks
If you are unsure of a task name, list tasks first:
./gradlew tasks
Then use the displayed task name with -x.
Step by Step Execution
Consider this command:
./gradlew build -x test
A simplified sequence is:
- Gradle loads the project configuration and plugins.
- Gradle receives
buildas the requested task. - Gradle finds the tasks required by
build, which commonly includeassembleandcheck. - During task-graph creation, Gradle removes the task named
testbecause of-x test. - Gradle runs remaining tasks, such as compiling production code and creating a JAR.
- The standard unit-test task is not executed.
For example, given a Java project, output may include tasks like these:
> Task :compileJava
> Task :processResources
> Task :classes
> Task :jar
> Task :assemble
> Task :check
> Task :build
BUILD SUCCESSFUL
Notice that :test is absent. The exact tasks vary by plugins and project configuration.
Real World Use Cases
Skipping tests can be useful in limited situations:
- Local iteration: You changed documentation, build metadata, or packaging configuration and only need to verify that the artifact can be assembled.
- Debugging build failures: You want to determine whether a failure occurs during compilation/packaging or during tests.
- Separate CI stages: A pipeline may compile and package in one job, while unit tests run in a dedicated test job.
- Temporary environment limitations: A test depends on an unavailable service or platform-specific dependency while you inspect a non-test build issue.
A successful build with -x test does not prove that the application works correctly. It proves only that the remaining selected tasks succeeded.
Real Codebase Usage
In real projects, developers usually keep the normal build command strict:
./gradlew build
They use exclusions temporarily or in explicitly named workflow commands:
./gradlew build -x test
Prefer a separate task for a repeated workflow
If a team often needs an artifact without verification, a clearly named task is easier to discover than asking everyone to remember command-line flags.
Kotlin DSL (build.gradle.kts):
tasks.register("assembleWithoutTests") {
group = "build"
description = "Assembles the project without running verification tasks."
dependsOn(tasks.named("assemble"))
}
Run it with:
./gradlew assembleWithoutTests
This is often preferable when the real requirement is packaging only. The assemble lifecycle task generally creates outputs without depending on check or test.
Property-controlled test disabling
A build can intentionally support a property, but it must be configured in the build script. For example, in Kotlin DSL:
Common Mistakes
Assuming any -D property changes Gradle
This command creates a system property but does not inherently skip tests:
gradle -Dskip.tests build
Use Gradle task exclusion instead:
./gradlew build -x test
Or configure the build explicitly to read a property.
Using Maven flags in Gradle
Maven and Gradle use different command-line conventions. Do not assume a Maven test-skipping property works in Gradle.
# Not a standard Gradle mechanism
./gradlew build -DskipTests
Use:
./gradlew build -x test
Expecting -x test to skip every kind of test
-x test excludes only the task named test. A project may also have tasks such as integrationTest, functionalTest, testDebugUnitTest, or plugin-specific verification tasks.
Inspect tasks and exclude the relevant ones deliberately:
Comparisons
| Command or approach | What it does | Best use |
|---|---|---|
./gradlew build | Runs the full build lifecycle, including standard tests. | CI, pull requests, release verification. |
./gradlew build -x test | Runs build but excludes the task named test. | Temporary local builds when unit tests are intentionally skipped. |
./gradlew assemble | Produces build outputs without running the normal verification lifecycle. | Packaging or checking whether production code assembles. |
./gradlew test | Runs the standard unit-test task only. | Fast feedback while developing tests. |
./gradlew build -PskipTests | Does nothing unless the build script or a plugin handles . |
Cheat Sheet
# Normal full build
./gradlew build
# Build without the standard unit-test task
./gradlew build -x test
# Build/package outputs without the verification lifecycle
./gradlew assemble
# Exclude multiple known test tasks
./gradlew build -x test -x integrationTest
# Discover task names
./gradlew tasks
-x taskNamemeans exclude this task.testis commonly the standard JVM unit-test task, but projects can define other test tasks.-Dproperty=valuesets a system property; it is not a built-in “skip tests” switch.-Pproperty=valuesets a project property; it also needs build-script support.- Prefer
./gradlewover a globally installedgradlecommand.
FAQ
How do I skip tests when running gradle build?
Run:
./gradlew build -x test
Why does -Dskip.tests not skip Gradle tests?
Gradle does not treat skip.tests as a built-in instruction. -D only provides a system property; a plugin or build script must read it before it affects tasks.
Does build -x test still compile test source code?
Usually, no test execution occurs, and test compilation tasks are commonly not needed once test is excluded. However, exact behavior depends on other tasks and custom dependencies in your build.
Does -x test skip integration tests?
Not necessarily. It excludes only the task named test. Exclude separately named tasks, such as integrationTest, if they are part of your build.
Is ./gradlew assemble the same as ./gradlew build -x test?
Not always. assemble focuses on producing outputs. build -x test still requests the lifecycle and may run verification tasks other than .
Mini Project
Description
Create a small Gradle Java project workflow that has both unit tests and an additional integration-test task. You will practice identifying task names and selectively excluding test tasks when you only need an artifact build.
Goal
Build the application JAR normally, then run an artifact-only build command that skips both unit and integration tests.
Requirements
- Create a Gradle Java project with a
testtask. - Add one passing unit test.
- Add an
integrationTesttask that runs a separate test source set. - Verify that
./gradlew buildruns the test tasks. - Run a build command that excludes both
testandintegrationTest. - Confirm that a JAR is created in
build/libs.
Keep learning
Related questions
Add External JAR Files to an IntelliJ IDEA Java Project
Learn how to add external JAR dependencies to an IntelliJ IDEA Java project using module libraries, and when to use Maven or Gradle instead.
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Call a Method After a Delay in Android Java
Learn how to run Java code after a delay in Android using Handler.postDelayed, manage the main thread, and cancel callbacks safely.