Gradle
Build Tools
Software Development
Testing
Programming

Gradle build without tests: skip tests with -x test

Codemia Knowledge Hub ·

To run a Gradle build without tests, pass -x test:

bash
gradle build -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.

bash
# See the full task graph without executing
gradle build --dry-run

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:

bash
1# Skip unit tests and integration tests
2gradle build -x test -x integrationTest
3
4# Skip several custom test tasks
5gradle build -x test -x functionalTest -x contractTest

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:

bash
gradle :api:build -x :api:test

Skipping tests with the Gradle wrapper

Most projects use the wrapper (gradlew). The syntax is the same:

bash
./gradlew build -x test

On Windows:

bash
gradlew.bat build -x test

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):

groovy
test {
    enabled = false
}

Kotlin DSL (build.gradle.kts):

kotlin
tasks.test {
    enabled = false
}

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

groovy
1if (project.hasProperty('skipTests')) {
2    test {
3        enabled = false
4    }
5}
bash
./gradlew build -PskipTests

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:

groovy
1test {
2    if (System.getProperty('skipTests') != null) {
3        enabled = false
4    }
5}
bash
./gradlew build -DskipTests

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:

yaml
1# GitHub Actions example
2jobs:
3  deploy:
4    steps:
5      - name: Build artifact without tests
6        run: ./gradlew build -x test
7
8      - name: Deploy
9        run: ./deploy.sh build/libs/*.jar

To decide in the build script itself:

groovy
1if (System.getenv('CI_SKIP_TESTS') == 'true') {
2    tasks.withType(Test) {
3        enabled = false
4    }
5}

tasks.withType(Test) disables all test tasks regardless of name, which is cleaner than listing each one.

Comparison of methods

MethodScopePersistentUse case
gradle build -x testSingle invocationNoQuick local build
-PskipTests with hasPropertySingle invocationNoTeam convention with explicit flag
test { enabled = false }All buildsYesNon-test modules
tasks.withType(Test) { enabled = false }All test tasksYesDisabling all tests globally
System.getenv checkEnvironment-dependentConditionalCI pipeline stages

What -x test skips and what still runs

TaskWith -x testWithout -x test
compileJavaRunsRuns
processResourcesRunsRuns
classesRunsRuns
compileTestJavaSkippedRuns
processTestResourcesSkippedRuns
testSkippedRuns
checkRuns (without test)Runs
jarRunsRuns
assembleRunsRuns
buildRuns (with exclusions)Runs (fully)

If you only need the JAR and not the check lifecycle, gradle assemble never includes tests:

bash
./gradlew assemble

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:

bash
./gradlew test --rerun-tasks

Or delete the previous build outputs first (this does not clear the build cache):

bash
./gradlew clean test

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.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms