I've implemented a Gradle plugin in Java. I followed this tutorial.
My plugin class looks as follows:
public class DemoPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getExtensions().create("DemoSetting", DemoPluginExtension.class);
project.getTasks().create("demo", DemoTask.class);
}
}
Currently I am intended to implement the onlyIf feature but unfortunately I did not succeed yet.
My try is the following:
public class DemoPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
DemoPluginExtension ext = project.getExtensions().create("demoSetting", DemoPluginExtension.class);
Closure closure = new Closure(this) {
public Object doCall() {
return ext.isExecuteTask();
}
};
project.getTasks().create("demoProperties", demoTask.class).onlyIf(closure);
}
}
The error I am observing is the following:
What went wrong: Execution failed for task ':demo'. Cannot call Task.setOnlyIf(Closure) on task ':demo' after task has started execution.
I would really appreciate if someone could give me a hint for solving the issue.
Finally I solved it using the following code:
Please note the implementation of the method
instead of