I want to throw an error while applying the plugin apply method to inform the user about the missing settings (and help them fix it). I repeat the following error:
Failed to notify ProjectEvaluationListener.afterEvaluate(), but primary configuration failure takes precedence.
I want to do two things:
- Report Error () appropriately
- Avoid creating a huge stack trace - only an exception message is required
The last comment in Gradle error 1617 says the following:
> It happens that closing afterEvaluate is done, although apply () throws an exception.
No wonder. The closure you give afterEvaluate () is a callback and fires even if the evaluation failed. You should check the project status ( http://www.gradle.org/docs/current/javadoc/org/gradle/api/Project.html#getState () ) in your afterEvaluate callback and return it to the failed state.
After several tests, I could not find the correct way to structure the call to getState ().
Summary of gradle plugin file:
class j2objc implements Plugin<Project> {
void apply(Project project) {
project.afterEvaluate {
def j2objcHome = System.getenv()['J2OBJC_HOME']
if (j2objcHome == null) {
throw new InvalidUserDataException(
'j2objc home not set, <helpful instructions to fix>')
}
project.tasks.create(name: 'j2objcTranslate', type: Exec) {
description 'Translates all the java source files in to Objective-C using j2objc'
}
}
}
}
The complete error, including the portion of the long stack trace that I would like to hide:
$ gradlew base:j2objcTranslate
Failed to notify ProjectEvaluationListener.afterEvaluate(), but primary configuration failure takes precedence.
org.gradle.api.InvalidUserDataException: j2objc home not set, this can be set using J2OBJC_HOME environment variable or within build.gradle by adding: j2objcConfig { j2objcHome = <path> }, e.g. j2objcConfig { j2objcHome = "$projectDir/../j2objc/"}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
<a huge number more lines>
Update
gradle . , ( ). , , , .
$ gradlew base:j2objcTranslate
FAILURE: Build failed with an exception.
* Where:
Script '/Users/brunobowden/dev/myproject/base/j2objc.gradle' line: 35
* What went wrong:
A problem occurred configuring project ':android'.
> A problem occurred configuring project ':base'.
> j2objc home not set, <helpful message>