I fail to understand where the classpath comes from.
I do understand that the plugin needs to bring the ant jar for the optional dependency ( org.apache.ant:ant-junit:1.9.7 in this case), but I always get the following error when running the task:
> Problem: failed to create task or type junitreport
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator was not found.
This looks like one of Ant's optional components.
Action: Check that the appropriate optional JAR exists in
-ANT_HOME/lib
-the IDE Ant configuration dialogs
Do not panic, this is a common problem.
The commonest cause is a missing JAR.
This is not a bug; it is a configuration problem
It doesn’t matter if my plugin declares the dependency as implementation, api, compileOnly or compileOnlyApi.
In the old groovy plugin it worked with runtime, but that is deprecated now
It is deprecated since many many many years already.
The old runtime is like the new runtimeOnly.
But if implementation and api which also do land on the runtimeClasspath do not work, neither runtime nor runtimeOnly would help.
The general documentation about using Ant from Gradle is at Using Ant from Gradle.
For how to use it from Java I showed you the example from Gradle sources.
If you need more help, you at least have to share an MCVE.