Antbuilder has no method "taskdef"

(Richard Schubert) #1


Inside a gradle Task object you can say something like:

According to the gradle specification on the Task class its property “ant” is of type “AntBuilder”.
However the gradle specifiaction on “Antbuilder” does not mention a method called “taskdef”. Why then can I say “ant.taskdef”?

For the specification on AntBuilder see:

(Mark Vieira) #2

This is actually a capability provided by Groovy. It’s a dynamic builder, similar to others provided by the language.

(Richard Schubert) #3

Hm if I say println(ant.getClass()), I get “org.gradle.api.internal.project.DefaultAntBuilder”.
However upon inspecting that classes code, neither it nor its superclasses have a method called “taskdef”.
Where does that “taskdef” method come from or if it is not a method, what is it technically?
You say it is provided by the Groovy language. But I cannot find a description of this kind of enhancement in the Groovy documentation.

(Mark Vieira) #4

It is effectively dynamically created at runtime. When you call ant.taskDef() Groovy is intercepting this method and instead of trying to execute some method named taskDef() it is building an internal XML like structure (just like an Ant build.xml) file. It then uses this data model to delegate to Ant itself.

(Richard Schubert) #5

Ok thank for your answer!

Would there have been any way for me to read about this behavior in some groovy documentation?

(Mark Vieira) #6

This is effectively an implementation detail. You can check out this article for more information on how Groovy builders work in general.