Antbuilder has no method "taskdef"

Hi!

Inside a gradle Task object you can say something like:
ant.taskdef(...)

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: https://docs.gradle.org/current/javadoc/org/gradle/api/AntBuilder.html#AntBuilder()

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

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.

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.

1 Like

Ok thank for your answer!

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

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