Gradle build without tests
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To run a Gradle build without tests, pass -x test on the command line:
The -x flag excludes a named task from the build execution graph. Since test is the standard task that runs unit tests, excluding it skips test compilation and execution while still compiling your main source code, processing resources, and producing the output artifact.
How Gradle Task Exclusion Works
Gradle builds are organized as a directed acyclic graph (DAG) of tasks. When you run gradle build, Gradle resolves all tasks that build depends on, including compileJava, processResources, classes, jar, test, and check. The -x flag removes a task and all tasks that exist solely because of that dependency.
When you exclude test, Gradle also skips check (which depends on test) and any custom tasks that depend exclusively on test. Tasks that test depends on, like compileTestJava, may still run if other tasks need them, but in practice they are usually pruned.
Excluding Multiple Test Tasks
Projects with integration tests, functional tests, or other custom test tasks need multiple exclusions:
For multi-module projects, the -x flag applies globally. To skip tests in a specific submodule only:
Using the Gradle Wrapper
Most projects use the Gradle wrapper (gradlew) instead of a system-installed Gradle. The syntax is identical:
On Windows:
Always prefer ./gradlew over gradle to ensure everyone on the team uses the same Gradle version.
Skipping Tests in build.gradle
Disabling the Test Task Permanently
If tests should never run as part of the default build (rare, but sometimes appropriate for documentation-only modules), disable the task in build.gradle:
In Kotlin DSL (build.gradle.kts):
Conditional Skipping With a Project Property
A more flexible approach is to skip tests only when a property is passed:
Then invoke the build with:
This keeps tests enabled by default but gives developers a quick escape hatch during iterative development.
Conditional Skipping With a System Property
System properties work similarly and integrate well with CI environment variables:
Skipping Tests in CI/CD Pipelines
In CI environments, you typically want tests to run on every build. But there are valid reasons to skip them in specific pipeline stages, such as a deployment step that only needs the artifact:
For branch-based skipping in the build script itself:
The tasks.withType(Test) selector disables all test tasks regardless of their name, which is cleaner than listing each one individually.
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 Gets Skipped vs. What Still Runs
Understanding exactly what -x test skips is important:
| 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 | Skipped (depends on test) | Runs |
jar | Runs | Runs |
assemble | Runs | Runs |
build | Runs (with exclusions) | Runs (fully) |
If you only need the JAR and do not need the check lifecycle at all, gradle assemble is an alternative that never includes tests:
This produces the same artifact as build without running any verification tasks.
The --no-build-cache and --rerun-tasks Interaction
When you skip tests during development and later run a full build, Gradle's build cache may report tests as "UP-TO-DATE" even though they never ran. This happens because the test inputs (compiled test classes) have not changed. To force tests to execute:
Or clear the build cache entirely:
Common Pitfalls
Skipping tests during development and forgetting to run them before pushing is the most common issue. The build passes locally because tests were excluded, but CI fails because it runs the full build. Make running ./gradlew build (with tests) a habit before committing.
Using test { enabled = false } in build.gradle and forgetting about it means tests silently stop running for everyone, including CI. This configuration should almost never be checked into a shared repository without a clear comment explaining why.
Excluding test but not integrationTest (or other custom test tasks) gives a false sense of speed when the integration tests are the slow part. List all test-type tasks with ./gradlew tasks --group verification to know what you need to exclude.
Running gradle assemble when you actually need build skips not just tests but also other verification tasks like checkstyle, pmd, or spotbugs. Make sure you are aware of what check includes before bypassing it entirely.
In multi-module builds, -x test applies globally. If you only want to skip tests for one module, use the fully qualified task name: -x :module-name:test.
Summary
- Use
./gradlew build -x testto exclude tests from a single build invocation. - Use
./gradlew assemblewhen you only need the artifact and no verification at all. - Add
-PskipTestssupport inbuild.gradlefor a team-friendly opt-in flag. - Use
tasks.withType(Test) { enabled = false }to disable all test tasks programmatically. - Always run the full build with tests before pushing to avoid CI surprises.
- Prefer the Gradle wrapper (
./gradlew) over the systemgradlebinary for reproducible builds.
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

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.