/commands/gradle/gradle-build-skip-tests
gradle build -x test: build a Gradle project without running tests
- tool
- Gradle
- versions
- Gradle 7.x, Gradle 8.x, Gradle 9.x
- os
- any
./gradlew build -x test runs the build task with the test task excluded: Gradle compiles, packages and runs the other checks, but no tests. -x (--exclude-task) removes a task and everything only it would need from the execution graph, and can be repeated. If you only want the JAR or distribution, ./gradlew assemble is shorter and also skips other verification tasks.
Usage
./gradlew build -x test # everything except unit tests
./gradlew build -x test -x integrationTest
./gradlew assemble # only build the outputs, no checks at all
./gradlew :app:build -x :app:test # one subproject
./gradlew build -x test --offline # also skip network access to repositoriesOn Windows use gradlew.bat build -x test (or .\gradlew build -x test in PowerShell). With a locally installed Gradle the command is gradle build -x test, but the wrapper guarantees the version the project expects.
Which command to use
| Command | Compiles main code | Compiles tests | Runs tests | Runs other checks |
|---|---|---|---|---|
| ./gradlew build | Yes | Yes | Yes | Yes |
| ./gradlew build -x test | Yes | Only if another task needs them | No | Yes |
| ./gradlew assemble | Yes | No | No | No |
| ./gradlew check -x test | Yes | Depends on the checks | No | Yes |
| ./gradlew jar | Yes | No | No | No (JAR only) |
Pitfalls
- Wrong task name.
-xneeds an existing task name;-x testsfails because the task is calledtest. List tasks with./gradlew tasks --all. - Not the same as -DskipTests. Gradle ignores Maven-style system properties.
-DskipTestsor-Dmaven.test.skipdo nothing unless the build script reads them; compare the Maven options in skipTests vs maven.test.skip. - Tests still compiled. Tasks like Checkstyle or SpotBugs for test sources still compile test code even with
-x test; a broken test file can still fail the build. - Hiding failures. Skipping tests to get a green build in CI hides regressions. Run a single failing test with gradle test --tests to fix it instead.
- Build failing anyway with errors such as Unsupported class file major version is unrelated to tests: it means the JDK is too new for the Gradle version.
How to confirm it worked
Run with --console=plain to see each task: :test does not appear in the list of executed tasks, while :compileJava, :jar and the other build tasks do. ./gradlew build -x test --dry-run (-m) prints the tasks that would run without running them.
FAQ
Is there a Gradle equivalent of Maven -DskipTests?
-x test is the usual equivalent: the test task does not run, but test classes are still compiled when another task needs them. To skip compiling them too, run a task that does not depend on tests, such as assemble.
What is the difference between gradle build -x test and gradle assemble?
assemble only produces the outputs (JARs, distributions). build -x test runs everything build normally runs, such as check tasks like linters or Checkstyle, except the test task itself.
How do I skip tests only in CI or only sometimes?
Use -x test on the command line in that job, or make the test task conditional in the build script, for example tasks.test { onlyIf { !project.hasProperty('skipTests') } } and run with -PskipTests.