[GRADLE-2317] Groovy AST transformations that refer to GroovyTestCase fail with a NoClassDefFoundError Created: 18/May/12  Updated: 04/Jan/13  Resolved: 25/May/12

Status: Resolved
Project: Gradle
Affects Version/s: 1.0-rc-3
Fix Version/s: 1.1-rc-1

Type: Bug
Reporter: Gradle Forums Assignee: Peter Niederwieser
Resolution: Fixed Votes: 0


 Description   

See GRADLE-2232 for details. I ran into this problem with gradle 1.0-rc-3. Reverting to rc2 fixed it, but I obviously didn't want to do it. Using the workaround recommended in these forums:

```groovy
compileTestGroovy

{ groovyOptions.useAnt = true }

```

worked just fine for me, but I'd prefer not to use a workaround. I'm on windows 7 x64 and CentOS 5 and 6 x64.



 Comments   
Comment by Gradle Forums [ 18/May/12 ]

Do you have a stack trace for the failure? You'll need to run with the --full-stacktrace command-line option to get this, as the regular --stacktrace option doesn't include the information we need.

Comment by Gradle Forums [ 18/May/12 ]

The failing line in our code is a dependency that references GroovyTestCase. junit is confirmed to be pulled in by the dependencies command, not to mention the fact that using the workaround does it just fine.

  • Exception is:
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':testproj:compileTestGroovy'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:68)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:34)
    at org.gradle.api.internal.changedetection.CacheLockHandlingTaskExecuter$1.run(CacheLockHandlingTaskExecuter.java:34)
    at org.gradle.cache.internal.DefaultCacheAccess$2.create(DefaultCacheAccess.java:200)
    at org.gradle.cache.internal.DefaultCacheAccess.longRunningOperation(DefaultCacheAccess.java:172)
    at org.gradle.cache.internal.DefaultCacheAccess.longRunningOperation(DefaultCacheAccess.java:198)
    at org.gradle.cache.internal.DefaultPersistentDirectoryStore.longRunningOperation(DefaultPersistentDirectoryStore.java:111)
    at org.gradle.api.internal.changedetection.DefaultTaskArtifactStateCacheAccess.longRunningOperation(DefaultTaskArtifactStateCacheAccess.java:83)
    at org.gradle.api.internal.changedetection.CacheLockHandlingTaskExecuter.execute(CacheLockHandlingTaskExecuter.java:32)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:55)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:57)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:41)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:42)
    at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:247)
    at org.gradle.execution.DefaultTaskGraphExecuter.executeTask(DefaultTaskGraphExecuter.java:192)
    at org.gradle.execution.DefaultTaskGraphExecuter.doExecute(DefaultTaskGraphExecuter.java:177)
    at org.gradle.execution.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:83)
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:36)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61)
    at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:67)
    at org.gradle.api.internal.changedetection.TaskCacheLockHandlingBuildExecuter$1.run(TaskCacheLockHandlingBuildExecuter.java:31)
    at org.gradle.cache.internal.DefaultCacheAccess$1.create(DefaultCacheAccess.java:111)
    at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAccess.java:126)
    at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAccess.java:109)
    at org.gradle.cache.internal.DefaultPersistentDirectoryStore.useCache(DefaultPersistentDirectoryStore.java:103)
    at org.gradle.api.internal.changedetection.DefaultTaskArtifactStateCacheAccess.useCache(DefaultTaskArtifactStateCacheAccess.java:79)
    at org.gradle.api.internal.changedetection.TaskCacheLockHandlingBuildExecuter.execute(TaskCacheLockHandlingBuildExecuter.java:29)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61)
    at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:67)
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:54)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:155)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:110)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:78)
    at org.gradle.launcher.cli.RunBuildAction.execute(RunBuildAction.java:42)
    at org.gradle.launcher.cli.RunBuildAction.execute(RunBuildAction.java:28)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:201)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:174)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:170)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:139)
    at org.gradle.launcher.exec.ExceptionReportingAction.execute(ExceptionReportingAction.java:31)
    at org.gradle.launcher.exec.ExceptionReportingAction.execute(ExceptionReportingAction.java:20)
    at org.gradle.launcher.Main.doAction(Main.java:48)
    at org.gradle.launcher.exec.EntryPoint.run(EntryPoint.java:45)
    at org.gradle.launcher.Main.main(Main.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.gradle.launcher.ProcessBootstrap.runNoExit(ProcessBootstrap.java:50)
    at org.gradle.launcher.ProcessBootstrap.run(ProcessBootstrap.java:32)
    at org.gradle.launcher.GradleMain.main(GradleMain.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:33)
    at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:124)
    at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:53)
    Caused by: java.lang.NoClassDefFoundError: junit/framework/TestCase
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:296)
    at org.gradle.util.FilteringClassLoader.loadClass(FilteringClassLoader.java:59)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:296)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:696)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:805)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at test.testing.transformations.TestTransformation.(TestTransformation.java:69)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:90)
    at org.codehaus.groovy.transform.ASTTransformationVisitor$2.call(ASTTransformationVisitor.java:172)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:967)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:546)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:524)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:501)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:480)
    at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:109)
    at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:39)
    at org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonServer.execute(CompilerDaemonServer.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.TypeCastDispatch.dispatch(TypeCastDispatch.java:30)
    at org.gradle.messaging.remote.internal.WorkerProtocol.handleIncoming(WorkerProtocol.java:53)
    at org.gradle.messaging.remote.internal.WorkerProtocol.handleIncoming(WorkerProtocol.java:31)
    at org.gradle.messaging.remote.internal.ProtocolStack$ProtocolStage.handleIncoming(ProtocolStack.java:167)
    at org.gradle.messaging.remote.internal.ProtocolStack$BottomStage.handleIncoming(ProtocolStack.java:277)
    at org.gradle.messaging.remote.internal.ProtocolStack$BottomConnection$1.run(ProtocolStack.java:299)
    at org.gradle.messaging.remote.internal.ProtocolStack$ExecuteRunnable.dispatch(ProtocolStack.java:120)
    at org.gradle.messaging.remote.internal.ProtocolStack$ExecuteRunnable.dispatch(ProtocolStack.java:116)
    at org.gradle.messaging.dispatch.AsyncDispatch.dispatchMessages(AsyncDispatch.java:132)
    at org.gradle.messaging.dispatch.AsyncDispatch.access$000(AsyncDispatch.java:33)
    at org.gradle.messaging.dispatch.AsyncDispatch$1.run(AsyncDispatch.java:72)
    at org.gradle.messaging.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
    Caused by: java.lang.ClassNotFoundException: junit.framework.TestCase
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    ... 54 more

BUILD FAILED

Total time: 10.579 secs

Comment by Gradle Forums [ 18/May/12 ]

Looks like it might be a different problem with the same symptom. Do you know where `test.testing.transformations.TestTransformation` comes from? Do you have the code, or can you tell me what it's doing on line 69?

Comment by Gradle Forums [ 18/May/12 ]

Sorry, I should have been clearer on that. The ```test.testing.transformations.TestTransformation``` is an AST transformation (modeled after grails TestForTransformation almost exactly), and it references the GroovyTestCase class as I mentioned above (not so clearly):

public static final ClassNode GROOVY_TEST_CASE_CLASS = new ClassNode(GroovyTestCase.class);

Comment by Peter Niederwieser [ 19/May/12 ]

This problem occurs when a transform statically references a class (from now on called "evil") residing in the Groovy Jar that in turn references a class residing outside the Groovy Jar. GroovyTestCase is (by far) the most prominent example for such a class, but there are quite a few others. (One other example is GroovyServlet.) Running such a transform does work (in most cases) with the groovyc command line compiler and the Ant groovyc task, but does not currently work with Gradle or GMaven.

The reason why the Ant groovyc task with default settings (includeAntRuntime=false and fork=false) can run such a transform is that it adds all compile dependencies to its own class loader. (Looking at the source code, I get the impression that this isn't intentional.) With fork=true, the situation is similar: both the compiler and the compile dependencies end up on the application class loader, hence everything works. With includeAntRuntime=false and fork=false, a separate class loader is created, and compilation fails. (Even worse, with includeAntRuntime=false the groovyc Ant task can't compile any transform. This explains why Gradle's Ant-based groovyc integration (which hardcodes includeAntRuntime=false and defaults to fork=true) fails to compile any transform if fork=false, a fact that has always baffled me.)

The groovyc command line compiler works in a similar fashion: it loads the Groovy Jar, dependent Jars (like ASM), and all compile dependencies with one and the same class loader. Because the command line compiler doesn't use groovy-all, it looks as if it will run into troubles if the compile class path contains an incompatible version of a library like ASM or commons-cli. (However I haven't verified this.)

The fundamental issue that makes life hard for Gradle (and everyone else who wants to separate class loading of the compiler from that of compile dependencies) is that the Groovy compiler isn't separated from the Groovy runtime and library, neither on a package nor on a Jar level. Potential solutions from a Gradle perspective:

1. Require transform authors not to statically reference "evil" Groovy classes from transforms. Usually this is easy to achieve. In this particular case, one can use ClassHelper.make("groovy.util.GroovyTestCase") instead of new ClassNode(groovy.util.GroovyTestCase).

2. Require transform users to put dependencies of "evil" classes on the groovy configuration. In this particular case, adding JUnit will solve the problem.

3. Feed ApiGroovyCompiler with a (hard-coded) list of classes that, although they reside in the Groovy Jar, should nevertheless be loaded with the transform class loader (rather than the class loader that loads the compiler classes). Essentially this is a pragmatic (and programmatic) separation of the Groovy compiler from the Groovy library, maintained in the Gradle codebase.

4. Do it like (Ant) groovyc and put all compile dependencies on the same class loader as the compiler itself.

Solution 1. has the drawback that the transform author has to be aware of the problem, and has to change his code although it works with (Ant) groovyc. Furthermore, it isn't applicable to existing transforms that have this problem.

Solution 2. puts the burden on users. It also pollutes the class path from which the compiler is loaded, albeit not as much as 4.

Solution 4. sounds like a big hack, but might work out in practice as long as the groovy configuration contains groovy-all, which inlines all mandatory dependencies in shaded form. Still, the fact that this solution makes it risky to use groovy instead of groovy-all is a breaking change. It will also require bigger changes to ApiGroovyCompiler, mainly because we will have to make sure that a new compiler daemon isn't started every time the compile class path changes.

This leaves solution 3., which on paper looks like the winner to me. I think it would just need a small enhancement to FilteringClassLoader that allows to exclude individual classes. It is quite possible that excluding just GroovyTestCase will completely solve the problem for all practical purposes.

On a side note, Groovy 2.0 will finally split up the Groovy codebase into several Jars. For example, there will be a groovy-test and a groovy-sql Jar. This will likely solve the problem for Gradle builds that are careful enough to only put the compiler-required Jars on the groovy configuration. But again, this is something one has to know and adapt to, and it might be inconvenient to use. My guess is that a single all-in-one Groovy Jar will continue to exist.

Comment by Peter Niederwieser [ 19/May/12 ]

I have a fix based on 3. that works fine for GroovyTestCase.

Comment by Adam Murdoch [ 20/May/12 ]

Option 3. looks like the best option to me. I think you should check your fix in (to master branch).

Generated at Wed Jun 30 12:18:40 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.