IllegalAccessError with Gradle AntBuilder and latest Groovy version

I ran into an issue when attempting to use version 2.4.3 of Groovy as a testCompile dependency in a project that has unit tests and code coverage using Clover and the gradle-clover-plugin (version 2.0.1). I worked around the first issue by manually adding the testCompile dependencies to the Ant class loader:

project.afterEvaluate {
    test.doFirst {
        ClassLoader antClassLoader = org.apache.tools.ant.Project.class.classLoader
        configurations.testCompile.each { file ->
            antClassLoader.addURL(file.toURI().toURL())
        }
    }
}

This exposed another issue:

11:07:54.644 [ERROR] [org.gradle.BuildExceptionReporter] Execution failed for task ':core:test'.
11:07:54.645 [ERROR] [org.gradle.BuildExceptionReporter] > tried to access class groovy.util.AntBuilderLocator from class groovy.util.AntBuilder

The stack trace reveals what might be an access issue between the Gradle AntBuilder and its parent, the Groovy AntBuilder:

11:07:54.663 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: java.lang.IllegalAccessError: tried to access class groovy.util.AntBuilderLocator from class groovy.util.AntBuilder
11:07:54.663 [ERROR] [org.gradle.BuildExceptionReporter] 	at org.gradle.api.AntBuilder.<init>(AntBuilder.java:23)
11:07:54.664 [ERROR] [org.gradle.BuildExceptionReporter] 	at org.gradle.api.internal.project.ant.BasicAntBuilder.<init>(BasicAntBuilder.java:34)
11:07:54.664 [ERROR] [org.gradle.BuildExceptionReporter] 	at org.gradle.api.internal.project.DefaultAntBuilder.<init>(DefaultAntBuilder.java:44)
11:07:54.664 [ERROR] [org.gradle.BuildExceptionReporter] 	at org.gradle.api.internal.project.DefaultAntBuilderFactory.create(DefaultAntBuilderFactory.java:37)
11:07:54.664 [ERROR] [org.gradle.BuildExceptionReporter] 	at org.gradle.api.internal.project.DefaultAntBuilderFactory.create(DefaultAntBuilderFactory.java:26)

The best that I can tell is that Gradle is currently built on 2.3.10 of Groovy. From looking at the Groovy source, the class in question (AntBuilderLocator) was present as a default/package-scoped class in 2.3.10, as well as in 2.4.3. I guess I have a few questions:

  1. Is there a better way to get Gradle to find the Groovy JAR from the testCompile classpath?
  2. Am I missing something with regards to the IllegalAccessError? Seems to me that should happen in any version of Gradle using the Groovy AntBuilder because of the packaged-scoped inner class.
  3. Has anyone else successfully been able to use Clover with a newer Groovy version in Gradle?

I should add that if the Groovy dependency is made a compile dependency, everything works without the afterEvaluate hack. However, that exposes Groovy as a transitive dependency to other projects that consume this project, even though Groovy is only being used for test purposes (not ideal).

1 Like