Hello, I’m try write my first plugin to capture the total buildtime and can’t figure out, why my extensions properties are always default / not set. Thanks for your advies.
GradlePlugin:
public class GradlePlugin implements Plugin<Project> {
@Override
public void apply(final Project project) {
final GradleExtension extension = project.getExtensions().create("gradleplugin", GradleExtension.class, project.getObjects());
project.getLogger().lifecycle("isEnabled: " + extension.isEnabled());
project.getLogger().lifecycle("getFoo: " + extension.getFoo());
registerBuildService(extension, gradle);
}
GradleExtension:
public class GradleExtension {
private final Property<Boolean> enabled;
private final Property<String> foo;
public GradleExtension(final ObjectFactory objects) {
enabled = objects.property(Boolean.class).convention(Boolean.TRUE);
foo = objects.property(String.class);
}
// getters
build.gradle.kts
plugins {
id("...gradleplugin") version "0.0.1-SNAPSHOT"
}
gradleplugin {
foo("bar")
}
Output:
> Configure project :gradle-plugin
isEnabled: true
getFoo: null
... many other tasks
BUILD SUCCESSFUL in 712ms
Because you register the extension and then immediately query the values and only after your apply method finishes the buildscript has a chance to set the values. Because of that you have the lazy Propertys and wire them to other properties that you then evaluate at execution time after the buildscript has a chance to set them.
You do various bad things with internal classes and also implement the wrong listener type.
You should reread the Shared Build Services documentation.
Don’t use GradleInternal and BuildEventListenerRegistryInternal but simply inject BuildEventsListenerRegistry.
Don’t implement BuildOperationListener, but OperationCompletionListener.
Do not use registry.onOperationCompletion, but registry.onTaskCompletion.
Now your build service should probably be seen as used until the build finished and your close method properly be called in the end of the build and seeing the configured values.
Actually I’m just missing the info about when the entire task finished as RunRootBuildWorkBuildOperationType.
Other things I found with DefaultTaskSuccessResult and DefaultTaskSkippedResult