# AntBuilder from Java

**URL:** <https://discuss.gradle.org/t/antbuilder-from-java/40482>\
**Category:** Help/Discuss\
**Created:** [July 14, 2021, 1:28pm UTC](https://discuss.gradle.org/t/antbuilder-from-java/40482 "2021-07-14T13:28:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Ebersole](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/steve_ebersole/32/9763_2.png) [@Steve\_Ebersole](https://discuss.gradle.org/u/Steve_Ebersole)\
**Post date:** [July 14, 2021, 1:28pm UTC](https://discuss.gradle.org/t/antbuilder-from-java/40482/1 "2021-07-14T13:28:18Z")

</div>

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:

```gradle
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?

---

<div class="post-metadata">

**Author:** ![Chris\_Dore](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/chris_dore/32/7592_2.png) [@Chris\_Dore](https://discuss.gradle.org/u/Chris_Dore)\
**Post date:** [July 16, 2021, 7:16am UTC](https://discuss.gradle.org/t/antbuilder-from-java/40482/2 "2021-07-16T07:16:43Z")

</div>

`Project.getAnt()` returns a [Gradle AntBuilder](https://docs.gradle.org/current/javadoc/org/gradle/api/AntBuilder.html) 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.

---

<div class="post-metadata">

**Author:** ![jjustinic](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jjustinic/32/9005_2.png) [@jjustinic](https://discuss.gradle.org/u/jjustinic)\
**Post date:** [July 16, 2021, 6:06pm UTC](https://discuss.gradle.org/t/antbuilder-from-java/40482/3 "2021-07-16T18:06:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve\_Ebersole](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/steve_ebersole/32/9763_2.png) [@Steve\_Ebersole](https://discuss.gradle.org/u/Steve_Ebersole)\
**Post date:** [July 19, 2021, 12:46pm UTC](https://discuss.gradle.org/t/antbuilder-from-java/40482/4 "2021-07-19T12:46:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve\_Ebersole](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/steve_ebersole/32/9763_2.png) [@Steve\_Ebersole](https://discuss.gradle.org/u/Steve_Ebersole)\
**Post date:** [July 19, 2021, 12:49pm UTC](https://discuss.gradle.org/t/antbuilder-from-java/40482/5 "2021-07-19T12:49:58Z")

</div>

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

```gradle
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( " " )
            }
        }
)

```
