Quick Fix: Execute the below command: gradle build -x test.
The Problem:
How to execute the gradle build
task without executing unit tests
The Solutions:
Solution 1: Gradle build without tests
To exlclude the unit test you can use the -x
command line argument, Here is how you can do it:
gradle build -x test
Update:
The Gradle user’s guide provides a diagram illustrating how the -x
argument works:
Solution 2: Using gradle assemble
To execute `gradle build` without executing the unit tests, use `gradle assemble`. To list all available tasks for your project, try:
“`shell
gradle tasks
“`
This command displays the list of available tasks, including `assemble`.
Solution 3: Exclude tests using build.gradle
To avoid executing unit tests during a `gradle build`, you can add the following lines to your `build.gradle` file:
test {
exclude '**/*'
}
This code excludes all files from the test execution. The double asterisks (**
) wildcard matches any directory level, and the asterisk (*
) wildcard matches any file or directory name. So, **/*
excludes all files in all subdirectories.
Solution 4: Add condition in all projects
The flag to skip tests in Gradle is -Dtest.single
, so to run the build without executing the unit tests, the syntax would be:
gradle -Dtest.single=false build
This flag can also be set in the Gradle.properties file, which makes it easier to toggle on and off without having to pass the flag each time:
systemProp.test.single=false
Solution 1: Using `-x test`
The -x test
flag excludes the test
task from the build, preventing unit tests from running. However, this approach also excludes test code compilation, which may be undesirable.
Command:
gradle build -x test
Solution 2: Using a custom task
Alternatively, you can create a custom task that depends on the build
task but excludes the test
task. This ensures that the project compiles without running unit tests.
Build.gradle:
task buildWithoutTests(dependsOn: build, doLast: {
tasks.test.skip()
})
Command:
gradle buildWithoutTests