Grails does not like loading the same class twice. I've observed this several times in Grails v1.1.1 and each time there was a different fix. The stack traces look pretty similar no matter what the root cause.
Example stack trace:
[groovyc] Compiling 33 source files to /Users/me/.grails/1.1.1/projects/trunk/classes Error executing script TestApp: : java.lang.LinkageError: loader constraint violation: loader (instance of
) previously initiated loading for a different type with name "org/w3c/dom/Attr" gant.TargetExecutionException: : java.lang.LinkageError: loader constraint violation: loader (instance of ) previously initiated loading for a different type with name "org/w3c/dom/Attr" at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:331) at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334) at gant.Gant$_dispatch_closure6.doCall(Gant.groovy) at gant.Gant.withBuildListeners(Gant.groovy:344) at gant.Gant.this$2$withBuildListeners(Gant.groovy) at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source) at gant.Gant.dispatch(Gant.groovy:334) at gant.Gant.this$2$dispatch(Gant.groovy) at gant.Gant.invokeMethod(Gant.groovy) at gant.Gant.processTargets(Gant.groovy:495) at gant.Gant.processTargets(Gant.groovy:480) Caused by: : java.lang.LinkageError: loader constraint violation: loader (instance of ) previously initiated loading for a different type with name "org/w3c/dom/Attr"
Case 1: /lib/ jar file conflicts with a Grails plugin
I was using a functional test plugin which included xerces. I already had xerces in the /lib folder of my grails project.
Solution to Case 1:
Remove the jar file from /lib as long as I was using this plugin... I found the conflicting jar file the crude way:
grep -r 'org.w3c.dom.Attr' ./ This solution is kind of messy but solved the problem until I stopped using this functional testing plugin at which time I had to add the jar back into the /lib/ folder.
In this case I was using a library of my own creation built in maven. To keep the dependency nightmare to a minimum, I used the super-jar provided by the assembly plugin to package all of the dependent classes into my library. This works great in most cases, but as it turns out one of the included libraries, Xerces, is also used by grails internally.
I discovered the conflicting jar files the same way as last time:
I didn't find a satisfactory solution to this issue. Lacking time to investigate the inclusion / exclusion features of the maven assembly plugin, I simply used the standard maven jar and selectively copied the dependencies into the
/lib folder of my grails project.
This 'solution' is fairly unsatisfying, but for now it works. Once I end up with dependency hell I'll revisit this issue and update this blog entry. In the mean time if anyone else solves this problem, please comment.Other Solutions
Although I have not run into any of these solutions, other people have had luck with them