Ant Task Troubleshooting
The Common Problem
very often user are talking that the groovy or groovyc tasks are failing with a ClassNotFoundException for the class GroovySourceAst
If it's failing with a ClassNotFoundException for a class other than GroovySourceAst, welcome to Ant. As the Ant manual for external tasks says, " Don't add anything to the CLASSPATH environment variable - this is often the reason for very obscure errors. Use Ant's own mechanisms for adding libraries." And in its library directories section, says "We get many more support calls related to classpath problems (especially quoting problems) than we like." So try running Ant as
ant -noclasspath, or even alias ant to that in your shell.
If the class that isn't found is GroovySourceAst and the above doesn't help, somewhere you have a conflicting antlr in your classpath. This may be because you are using maven and one of this parts is polluting the classpath or you have a different antlr jar in your classpath somewhere.
Solution 1: groovy-all
Use the groovy-all-VERSION.jar from the groovy distribution and not the normal groovy jar. The groovy-all-VERSION.jar does already contain antlr and asm libs in a seperate namespace so there should be no conflict with other libs around.
Solution 2: using loaderref
Sometimes it's not possible to use the groovy-all-VERSION.jar, for example because you want to build groovy before creating the jar. In this case you have to add a loaderref to the task definition. But that alone will not help. You have to add the rootLoaderRef task to set this loader reference. For example:
The groovy task will now be created using the tmp.groovy.groovyc class loader, which tries to avoid loading conflicting jars like antlr. It's important to execute the rootLoaderRef task once before the taskdef using the loaderref defined by the rootLoaderRef.
No, both Solutions will not help if you have conflicting ant jars or common-logging jars somewhere. Solution 2 is able to solve much more difficult jar problems as long as your classpath is as clean as possible. if you want to be on the safe side you have to fork the javaVM which means you have to use the task like this: