Gradle build without tests: skip tests with -x test
Codemia Knowledge Hub ·
To run a Gradle build without tests, pass -x test:
-x excludes a named task from the execution graph. Because test is the standard task that runs unit tests, excluding it skips test compilation and execution while still compiling main sources, processing resources and producing the artifact.
How -x excludes tasks
A Gradle build is a directed acyclic graph (DAG) of tasks. gradle build resolves everything build depends on, including compileJava, processResources, classes, jar, test and check. -x removes a task and all tasks that exist only because of that dependency.
Excluding test also drops the tasks that only test needs, such as compileTestJava and processTestResources, unless another task in the graph still requires them. Tasks that depend on test, like check, still run; they simply no longer trigger the tests, so other verification such as checkstyle still executes.
Skipping several test tasks
Projects with integration, functional or other custom test tasks need one -x per task:
List the test-type tasks with ./gradlew tasks --group verification so you know what to exclude; skipping test but not a slow integrationTest saves little time.
In multi-module projects -x test applies globally. To skip tests in one submodule only, use the qualified task name:
Skipping tests with the Gradle wrapper
Most projects use the wrapper (gradlew). The syntax is the same:
On Windows:
Prefer ./gradlew over gradle so everyone on the team uses the same Gradle version.
Skipping tests in build.gradle
Disabling the test task permanently
For modules where tests should never run in the default build (rare, but sometimes right for documentation-only modules):
Kotlin DSL (build.gradle.kts):
If this is checked into a shared repository, tests silently stop running for everyone, including CI. Add a clear comment explaining why.
Skipping only when a project property is passed
Tests stay on by default, with a quick escape hatch during iterative development.
Skipping with a system property
System properties work the same way and fit well with CI environment variables:
Skipping tests in CI/CD pipelines
CI should normally run tests on every build, but some stages, such as a deployment step that only needs the artifact, can skip them:
To decide in the build script itself:
tasks.withType(Test) disables all test tasks regardless of name, which is cleaner than listing each one.
Comparison of methods
| Method | Scope | Persistent | Use case |
gradle build -x test | Single invocation | No | Quick local build |
-PskipTests with hasProperty | Single invocation | No | Team convention with explicit flag |
test { enabled = false } | All builds | Yes | Non-test modules |
tasks.withType(Test) { enabled = false } | All test tasks | Yes | Disabling all tests globally |
System.getenv check | Environment-dependent | Conditional | CI pipeline stages |
What -x test skips and what still runs
| Task | With -x test | Without -x test |
compileJava | Runs | Runs |
processResources | Runs | Runs |
classes | Runs | Runs |
compileTestJava | Skipped | Runs |
processTestResources | Skipped | Runs |
test | Skipped | Runs |
check | Runs (without test) | Runs |
jar | Runs | Runs |
assemble | Runs | Runs |
build | Runs (with exclusions) | Runs (fully) |
If you only need the JAR and not the check lifecycle, gradle assemble never includes tests:
It produces the same artifact as build without any verification tasks. That also skips other checks wired into check, such as checkstyle, pmd or spotbugs, so know what check includes before bypassing it.
Tests reported UP-TO-DATE after skipping
A later full build may report test as "UP-TO-DATE" or "FROM-CACHE" when its inputs match an earlier successful run, so the tests do not execute again. To force them to execute:
Or delete the previous build outputs first (this does not clear the build cache):
Run the full build before pushing
The most common problem is skipping tests locally and forgetting to run them before pushing: the local build passes, then CI fails on the full build. Make ./gradlew build (with tests) a habit before committing.
Related reading
- Gradle Could not determine java version from '11.0.2
- Gradle does not find tools.jar
- Gradle DSL method not found 'runProguard
- Gradle Execution failed for task 'processDebugManifest
- Have Docker wait for Kafka to startup before running tests
- have some one tried running cadence on cockroach db
- Gradle finds wrong JAVA_HOME even though it's correctly set
- Gradle Implementation vs API configuration
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.