Hello!
I’m in the process of writing some of my first Gradle binary plugins using Groovy as a basis (using the groovy-gradle-plugin
to access the API).
I’m struggling to implement a plugin with an extension that provides lazy configuration properties. What I’m seeing is that when I provide a convention on the Property, that value is always being used, despite a build.gradle
that consumes the plugin specifying a different value.
MyPlugin.groovy
:
abstract class MyPluginExtension {
abstract Property<Boolean> feature
MyPluginExtension(ObjectFactory factory) {
feature = factory.property(Boolean)
}
}
class MyPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
def extension = project.extensions.create('myplugin', MyPluginExtension, project.objects)
extension.feature.convention(false)
if (extension.feature.get()) {
println("Feature is enabled")
} else {
println("Feature is disabled")
}
}
}
build.gradle
(Consumer):
plugins {
id 'myplugin' version '1.0-SNAPSHOT'
}
myplugin {
feature.set(true)
}
And the output when running shows this:
> Configure project :myproject
Feature is disabled
The Implementing Gradle Plugins page only describes implementing a binary plugin in Java - I’m unsure if that’s causing any of the issue I’m seeing: Implementing Gradle plugins.
I may also be combining ideas here, but the Lazy Configuration page shows it being implemented as an Interface instead: Lazy Configuration
I’ve found this Kotlin example which looks very similar to what I’d like to achieve: Gradle custom extension with lazy configuration doesn't work - #2 by Vampire
And if it’s not already obvious, I’d like to avoid project.afterEvaluate
at all costs