AntBuilder from Java

I am having a hard time figuring out how to convert some Groovy-based build code to Java when the Groovy code is using AntBuilder. Apparently y’all or Groovy weave extra capabilities into it.

E.g. I have this Groovy code:

project.ant.taskdef(name: 'xjc', classname: extension.xjcTaskName, classpath: configuration.asPath)

project.ant.xjc(
        destdir: extension.outputDir,
        binding: descriptor.xjcBinding,
        schema: descriptor.xsd,
        target: descriptor.jaxbVersion,
        extension: 'true') {
                ...
}

How can I achieve this in Java code?

Project.getAnt() returns a Gradle AntBuilder that extends the Groovy AntBuilder. You’ll want to use the invoke( "xjc", args ) method of the AntBuilder. I believe args should be a Map containing the same KVs you have in your groovy code, but I’m not 100% sure of that.

I have internal code that calls xjc from Java as well. I haven’t looked at in a while, but if the groovy code has a closure at the end where your ... starts, you end up wanting a List. The first element of the list is the Map mentioned, and the second is your code that represents what would be in the closure.

But what about the task def? Do I use invoke as well?

The ... would be Ant task args (<arg/>):

project.ant.xjc(
        destdir: extension.outputDir,
        binding: descriptor.xjcBinding,
        schema: descriptor.xsd,
        target: descriptor.jaxbVersion,
        extension: 'true') {
            arg line: "-npa "
            if ( !schemaDescriptor.xjcExtensions.empty ) {
                arg line: schemaDescriptor.xjcExtensions.collect { "-X${it}" }.join( " " )
            }
        }
)