Recommended way to stop a Gradle build
If you want to stop the build, throw:
throw new GradleException('error occurred')
or throw the subclasses for the above exception. Some of the subclass exceptions actually only fail the current task but continue with the build.
I usually throw the relevant exception from the org.gradle.api
package, for example InvalidUserDataException
for when someone has entered something invalid, or GradleScriptException
for more general errors.
If you want to stop the current task or action, and move on to the next, you can also throw a StopActionException
There is currently no dedicated method, although there have been discussions to add one.
The recommended way to stop a Gradle build is to throw an exception. Since Groovy doesn't have checked exceptions, and Gradle by default does not print the exception type, it's not that critical which exception is thrown. In build scripts, GradleException is often used, but a Groovy assertion also seems reasonable (depending on the circumstances and audience). What's important is to provide a clear message. Adding a cause (if available) helps for debugging (--stacktrace
).
Gradle provides dedicated exception types StopExecutionException
/StopActionException
for stopping the current task/task action but continuing the build.