I'm getting an error running a task defined in my ...
# gradle
m
I'm getting an error running a task defined in my gradle plugin:
Copy code
Execution failed for task ':setupMetadata'.
> Extension of type 'KPyExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension]
but in my plugin I have
Copy code
// fun Project.setupExtensions(), called first
extensions.create<KPyExtension>("kpy")  // register extension

// fun Project.setupTasks(), called later
task<Task>("setupMetadata") {
    actions.add {
        val target = kotlinExtension.targets.first { it is KotlinNativeTarget } as KotlinNativeTarget
        val ext = the<KPyExtension>()  // error here I'm assuming
        // task action
    }
}
I am able to use the
kpy
keyword in my build.gradle.kts file to configure it and in other parts of the plugin it works fine
e
probably it's looking for the extension on the wrong object, try
project.the<KPyExtension>()
v
It is, task is extension aware, so you are looking in the task extensions instead of in the project extensions. Btw. You shouldn't use
actions.add
but
doLast
, and you are using eager
task(...)
method, not leveraging task configuration avoidance with
tasks.register
e
or write a task class instead of an ad-hoc one like this, it's easy and will allow for custom properties to be configured by the extension, caching, etc.
v
Yes, definitely. But then still use
register
to create it 🙂