Getting exceptions when using ProjectBuilder to test gradle plugins after moving from Gradle 1.1 to 1.2

After moving from Gradle 1.1 to 1.2 we are seeing exceptions when testing our plugins.

This is the line of code that is generating the exception

def proj = ProjectBuilder.builder().withName("SomeProjectName").withProjectDir(new File("target/resources/test", "SomeProjectName")).build()

This is the exception

Running test: test testBasePluginExposesProperties(org.tiaa.reco.build.plugins.TiaaBasePluginTester)
  org.tiaa.reco.build.plugins.TiaaBasePluginTester > testBasePluginExposesProperties FAILED
    org.gradle.api.GradleException: Could not generate a proxy class for class org.gradle.api.internal.project.DefaultPr
oject.
        at org.gradle.api.internal.AbstractClassGenerator.generate(AbstractClassGenerator.java:187)
        at org.gradle.api.internal.ClassGeneratorBackedInstantiator.newInstance(ClassGeneratorBackedInstantiator.java:36
)
        at org.gradle.api.internal.project.ProjectFactory.createProject(ProjectFactory.java:47)
        at org.gradle.api.internal.project.ProjectFactory.createProject(ProjectFactory.java:31)
        at org.gradle.testfixtures.internal.ProjectBuilderImpl.createProject(ProjectBuilderImpl.java:72)
        at org.gradle.testfixtures.ProjectBuilder.build(ProjectBuilder.java:99)
        at org.tiaa.reco.build.plugins.AbstractPluginTester.getProject(AbstractPluginTester.groovy:14)
        at org.tiaa.reco.build.plugins.TiaaBasePluginTester.testBasePluginExposesProperties(TiaaBasePluginTester.groovy:
19)
          Caused by:
        java.lang.IncompatibleClassChangeError: Found interface org.objectweb.asm.MethodVisitor, but class was expected
            at org.gradle.api.internal.AsmBackedClassGenerator$ClassBuilderImpl.addGetter(AsmBackedClassGenerator.java:4
70)
            at org.gradle.api.internal.AsmBackedClassGenerator$ClassBuilderImpl.addGetter(AsmBackedClassGenerator.java:4
64)
            at org.gradle.api.internal.AsmBackedClassGenerator$ClassBuilderImpl.mixInGroovyObject(AsmBackedClassGenerato
r.java:430)
            at org.gradle.api.internal.AbstractClassGenerator.generate(AbstractClassGenerator.java:75)
            ... 7 more

We ran into a similar problem. We use Cobertura to measure code coverage, don’t know if you happen to do the same? Here is the relevant forum discussion.

I did see that post - we are not using cobertura, although we are using jacoco. Its only when using the ProjectBuilder.build() method that we run into this problem - when I take my custom distro and run a build of another project, all of the instrumentation & everything works fine.

It’s the same problem as with Cobertura. JaCoCo uses ASM in the same way.

From memory, you need to block JaCoCo pulling in its asm dependency during test time. In the Cobertura plugin, I rewrote it to ensure that cobertura and its dependencies are added to the runtime classpath after the actual runtime dependencies. This will fix it too.

So how do I go ahead and fix this issue? I am placing jacoco into its own configuration and then adding the dependencies to that configuration. How do I make sure it is added to the classpath after all of the other runtime dependencies?

It’s hard to say without knowing how your project is configured, but something like…

test {
  classpath = configurations.runtime + configurations.jacoco
}

I figured out my issue. Like Dan, I had originally included the jacoco libraries in my testRuntime configuration (I just scan for all .jar files in a lib/testRuntime directory for the testRuntime configuration). I was keeping the jacoco jars in that testRuntime directory, even though I had then created a separate configuration for them. I took them out of the testRuntime directory and now everything seems to work fine. Thanks!