Running sonar in Jenkins/hudson: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory
When we run sonar:sonar in Jenkins/hudson with maven, we get the following error:
org.dom4j.DocumentException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory Nested exception:
org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory
I googled the message, and find it is a known problem for running jenkins with sonar:
But the bug is closed without fixing. I compared the binary class of DocumentFactory in jars in maven’s repository and the one in hudson’s WEB-INF/lib directory, there are almost identical, so I believe it should be a problem caused by classloading, object created by a different classloader could not be cast to object of the same class (will raise ClassCastException).
After a little hacking, I fixed the problem by modified 2 source files in dom4j. You can fix the problem by download my modified version of dom4j-1.6.1.jar , just download it and replace hudson’s dom4j-1.6.1-hudson-3.jar with the new one (modified java source code is included within the jar):
mv /home/hudson/tomcat/webapps/hudson/WEB-INF/lib/dom4j-1.6.1-hudson-3.jar /home/hudson/dom4j-1.6.1-hudson-3.jar
mv dom4j-1.6.1.jar /home/hudson/tomcat/webapps/hudson/WEB-INF/lib/dom4j-1.6.1-hudson-3.jar
ps, if error still occur, please replace the dom4j-1.6.1.jar in your .m2 repository with the modifed dom4j-1.6.1.jar .