Using Ant task from external JAR with configuration cache

I am using an Ant method from an external JAR in a Gradle build, and I can’t work out how to get it to work with the configuration cache enabled. Is this possible, or is it something that isn’t compatible with the configuration cache (as warned about here)?

In more detail, I am pulling in the Ant task like this:

ant.withGroovyBuilder {
    "taskdef"( "name"      to "xmlbean",
               "classname" to "org.apache.xmlbeans.impl.tool.XMLBean",
               "classpath" to xbeanconf.asPath )
}

as documented in the XMLBeans documentation for the Ant task

I am defining a custom Gradle task type that makes use of this task:

abstract class ScompJar: Scomp() {
    @get:OutputFile
    abstract val jar: RegularFileProperty

    @TaskAction
    fun execute() {
        ant.withGroovyBuilder {
            "xmlbean"( "srcgendir"   to srcgendir.asFile.get(),
                       "classgendir" to classgendir.asFile.get(),
....

and registering a task of this type like this:

val scompSimple = tasks.register<ScompJar>("scompSimple") {
....

Without the configuration cache, this works perfectly, and has done over several previous versions of Gradle. With the configuration cache enabled, I get an error like this:

> Problem: failed to create task or type xmlbean
  Cause: The name is undefined.
  Action: Check the spelling.
  Action: Check that any custom tasks/types have been declared.
  Action: Check that any <presetdef>/<macrodef> declarations have taken place.

I’m guessing that this is because in the serialisation of the task’s state in the configuration cache, the Ant task “xmlbean” defined in the project’s AntBuilder is no longer visible to the task (but maybe I’m wrong here).

I have tried a number of ways of trying to get the xmlbean task definition into the task type, but so far I have not found an approach that works, and this is all some way outside my comfort zone. Is what I am trying to possible, and does anyone have any suggestions about how to do it? I am using Gradle 9.5.1.

(as warned about here)?

What do you mean?
The only thing I see is about importing an Ant build, not anything about using Ant tasks.

ant.withGroovyBuilder {

You mean you do that outside the task in the configuration phase or what do you mean?
If so, this has several drawbacks, no matter whether CC is in play.

  1. You always do it even if the task is not going to be used or even configured
  2. You resolve the xbeanconf at configuration time which also is a bad idea and there is even an idea to forbid this.

And with CC in play, this will also be the reason it is failing for you, because latest when the CC entry is reused, the configuration phase is not running and I highly doubt which task definitions where done is serialized into the CC entry.

Maybe this could be added, maybe not, but even if it could, you still have the other drawbacks at least.

I have tried a number of ways of trying to get the xmlbean task definition into the task type, but so far I have not found an approach that works

Next time, please list what you tried and why it did not work, then you can probably easily be pointed at a solution.
I for example would have been able to answer hours ago from mobile when I have read your post the first time, without trying it out myself first.

And also, just doing it just works.

val xbeandepconf = configurations.dependencyScope("xbeandepconf")
val xbeanconf = configurations.resolvable("xbeanconf") {
    extendsFrom(xbeandepconf)
    attributes {
        attribute(CATEGORY_ATTRIBUTE, project.objects.named(LIBRARY))
        attribute(USAGE_ATTRIBUTE, project.objects.named(JAVA_RUNTIME))
        attribute(LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(JAR))
        attribute(BUNDLING_ATTRIBUTE, project.objects.named(EXTERNAL))
        attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, project.objects.named(STANDARD_JVM))
    }
}

dependencies {
    xbeandepconf("org.apache.xmlbeans:xmlbeans:5.4.1")
}

abstract class ScompJar: DefaultTask() {
    @get:Classpath
    abstract val xbeanClasspath: ConfigurableFileCollection

    @get:InputDirectory
    abstract val srcgendir: DirectoryProperty

    @get:InputDirectory
    abstract val classgendir: DirectoryProperty

    @get:OutputFile
    abstract val jar: RegularFileProperty

    @TaskAction
    fun execute() {
        ant.withGroovyBuilder {
            "taskdef"( "name"      to "xmlbean",
                "classname" to "org.apache.xmlbeans.impl.tool.XMLBean",
                "classpath" to xbeanClasspath.asPath,
                "loaderref" to xbeanClasspath.asPath )
        }

        ant.withGroovyBuilder {
            "xmlbean"( "srcgendir"   to srcgendir.asFile.get(),
                "classgendir" to classgendir.asFile.get())
        }
    }
}

tasks.register<ScompJar>("scompSimple") {
    xbeanClasspath.from(xbeanconf)
    srcgendir.set(layout.buildDirectory.dir("scomp-simple/src"))
    classgendir.set(layout.buildDirectory.dir("scomp-simple/class"))
    jar.set(layout.buildDirectory.file("scomp-simple.jar"))
}

tasks.register<ScompJar>("scompSimple2") {
    xbeanClasspath.from(xbeanconf)
    srcgendir.set(layout.buildDirectory.dir("scomp-simple/src"))
    classgendir.set(layout.buildDirectory.dir("scomp-simple/class"))
    jar.set(layout.buildDirectory.file("scomp-simple2.jar"))
}

Sorry, a combination of tunnel vision and frustration took hold of me here… Your comments are helpful even so, and I am now working on fixing the next issue.