Reputation: 60594
I'm writing a plugin in which do something like the following:
project.extensions.create('myExtension', new MyExtension(project))
where MyExtension
is the class that defines my new functionality.
Now, in gradle.build
I can do this:
myExtension {
// configure cool stuff
}
What I would like to do now, is to "consume" a couple of the things in this configure closure, and pass the rest of the closure as-is to a task I defined, using project.configure(myTask, closure)
. However, I have no idea how to
Access the configure closure from the MyExtension
class.
"Consume" some of the closure, i.e. access some of the properties on the closure and then strip them, leaving another closure which has all the untouched things but nothing else
Any pointers would be greatly appreciated =)
Upvotes: 3
Views: 2448
Reputation: 894
One thing you can do is to call a method on your extension and provide the "configuration" as a closure. So instead of
// build.gradle
myExtension {
// configure cool stuff
}
you write
// build.gradle
myExtension.config {
// configure cool stuff
}
The according method in your extension class would look like this:
// MyExtension.groovy
class MyExtension {
Project project
MyExtension(Project project) {
this.project = project
}
def config(Closure cl) {
// Do stuff with the closure
}
}
However, as the whole closure is forwarded to your own code, you lose all the evaluation Gradle normally does with the configuration closure.
Upvotes: 0
Reputation: 123910
It's not how extensions work. The closure gets evaluated immediately in order to configure the extension object. After that, the closure is gone. Typically, a plugin will use the (information contained in the) extension object to further configure tasks.
PS: It's extensions.create('myExtension', MyExtension, project)
, not project.extensions.create('myExtension', new MyExtension(project))
.
Upvotes: 1