Running sonar in Jenkins/hudson: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory

April 20, 2011 by
Filed under: java, jenkins 

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:

https://issues.jenkins-ci.org/browse/JENKINS-8327

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):

wget http://www.beyondlinux.com/files/dom4j-1.6.1.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 .

 

 

 

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Comments

9 Comments on Running sonar in Jenkins/hudson: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory

  1. romaintaz on Thu, 16th Jun 2011 6:05 pm
  2. Hello,

    Thanks for your hack, it helps me to solve my issue!

    However, could you be a little more precise regarding the modifications you made?
    As Jenkins / Hudson is packaged with his own dom4j version (dom4j-1.6.1-hudson-3.jar), maybe it will be useful if you submit your modification as a patch. The defect #8327 has been rejected, but the #7666 seems to be related to this problem, and is still open:
    https://issues.jenkins-ci.org/browse/JENKINS-7666

    Regards.

    Romain

  3. euler on Thu, 16th Jun 2011 9:55 pm
  4. Hello, Romain,
    I’ve attached the source code in the jar, hope that help.

  5. Tomi Savolainen on Wed, 29th Feb 2012 4:55 am
  6. Thanks, your modified dom4j helped me! Nice work.

  7. My Homepage on Wed, 14th Mar 2012 5:00 am
  8. … [Trackback]…

    […] Read More: beyondlinux.com/2011/04/20/running-sonar-in-jenkinshudson-org-dom4j-documentfactory-cannot-be-cast-to-org-dom4j-documentfactory/ […]…

  9. Jakub Karbowiak on Tue, 3rd Apr 2012 5:05 pm
  10. Hello,

    your solution seems to be the only one for this problem. I would like to use it in my project. I’ve been trying to download the modified file, but I couldn’t access it. Could you please fix the link or send me the file to my email? I would be very grateful.

    Thank you in advance.

    Regards,
    Jakub

  11. David Göransson on Fri, 27th Apr 2012 8:00 pm
  12. Hi

    Your fix used to solve this problem for me. However, after updating Jenkins to 1.460 the problem persists even if I replace the jar file.

    Has something changed that needs this fix to be done again somehow?

    Thanks a bunch!
    David

  13. euler on Sat, 28th Apr 2012 3:58 pm
  14. Could you paste the error detail messages? try to add -e -X to your maven paramenter, and run jenkins you can see the errors.

  15. David Göransson on Mon, 30th Apr 2012 2:29 pm
  16. [ERROR] BUILD ERROR
    [INFO] ————————————————————————
    [INFO] Can not execute Sonar

    Embedded error: Unable to execute maven plugin
    org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory
    [INFO] ————————————————————————
    [DEBUG] Trace
    org.apache.maven.lifecycle.LifecycleExecutionException: Can not execute Sonar
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:284)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.lifecycle.LifecycleExecutorInterceptor.execute(LifecycleExecutorInterceptor.java:65)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at hudson.maven.agent.Main.launch(Main.java:185)
    at hudson.maven.MavenBuilder.call(MavenBuilder.java:151)
    at hudson.maven.Maven2Builder.call(Maven2Builder.java:77)
    at hudson.maven.Maven2Builder.call(Maven2Builder.java:53)
    at hudson.remoting.UserRequest.perform(UserRequest.java:118)
    at hudson.remoting.UserRequest.perform(UserRequest.java:48)
    at hudson.remoting.Request$2.run(Request.java:287)
    at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
    Caused by: org.apache.maven.plugin.MojoExecutionException: Can not execute Sonar
    at org.codehaus.mojo.sonar.Bootstraper.executeMojo(Bootstraper.java:103)
    at org.codehaus.mojo.sonar.Bootstraper.start(Bootstraper.java:79)
    at org.codehaus.mojo.sonar.SonarMojo.execute(SonarMojo.java:88)
    at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
    at hudson.maven.agent.PluginManagerInterceptor.executeMojo(PluginManagerInterceptor.java:182)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
    … 28 more
    Caused by: org.sonar.api.utils.SonarException: Unable to execute maven plugin
    at org.sonar.batch.AbstractMavenPluginExecutor.execute(AbstractMavenPluginExecutor.java:54)
    at org.sonar.batch.AbstractMavenPluginExecutor.execute(AbstractMavenPluginExecutor.java:38)
    at org.sonar.batch.phases.InitializersExecutor.executeMavenPlugin(InitializersExecutor.java:73)
    at org.sonar.batch.phases.InitializersExecutor.execute(InitializersExecutor.java:60)
    at org.sonar.batch.phases.Phases.execute(Phases.java:90)
    at org.sonar.batch.bootstrap.ProjectModule.doStart(ProjectModule.java:143)
    at org.sonar.batch.bootstrap.Module.start(Module.java:89)
    at org.sonar.batch.bootstrap.BatchModule.analyze(BatchModule.java:110)
    at org.sonar.batch.bootstrap.BatchModule.analyze(BatchModule.java:105)
    at org.sonar.batch.bootstrap.BatchModule.analyze(BatchModule.java:105)
    at org.sonar.batch.bootstrap.BatchModule.doStart(BatchModule.java:100)
    at org.sonar.batch.bootstrap.Module.start(Module.java:89)
    at org.sonar.batch.bootstrap.BootstrapModule.doStart(BootstrapModule.java:96)
    at org.sonar.batch.bootstrap.Module.start(Module.java:89)
    at org.sonar.batch.Batch.execute(Batch.java:74)
    at org.sonar.maven.SonarMojo.executeBatch(SonarMojo.java:154)
    at org.sonar.maven.SonarMojo.execute(SonarMojo.java:143)
    at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
    at hudson.maven.agent.PluginManagerInterceptor.executeMojo(PluginManagerInterceptor.java:182)
    at org.codehaus.mojo.sonar.Bootstraper.executeMojo(Bootstraper.java:98)
    … 33 more
    Caused by: org.apache.maven.lifecycle.LifecycleExecutionException: Internal error in the plugin manager executing goal ‘org.apache.maven.plugins:maven-surefire-plugin:2.12:test’: Failed to read /var/lib/jenkins/workspace/eSales Server Trunk Nightly/trunk/esales_query_processor/esales_query_processor_ingredient/target/surefire-reports/TEST-com.apptus.esales.FunctionTest.xml
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:698)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.forkProjectLifecycle(DefaultLifecycleExecutor.java:1205)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.forkLifecycle(DefaultLifecycleExecutor.java:1038)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:643)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.lifecycle.LifecycleExecutorInterceptor.execute(LifecycleExecutorInterceptor.java:65)
    at org.sonar.maven.Maven2PluginExecutor.concreteExecute(Maven2PluginExecutor.java:52)
    at org.sonar.batch.AbstractMavenPluginExecutor.execute(AbstractMavenPluginExecutor.java:52)
    … 52 more
    Caused by: org.apache.maven.plugin.PluginManagerException: Failed to read /var/lib/jenkins/workspace/eSales Server Trunk Nightly/trunk/esales_query_processor/esales_query_processor_ingredient/target/surefire-reports/TEST-com.apptus.esales.FunctionTest.xml
    at hudson.maven.agent.PluginManagerInterceptor.executeMojo(PluginManagerInterceptor.java:196)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
    … 64 more
    Caused by: hudson.util.IOException2: Failed to read /var/lib/jenkins/workspace/eSales Server Trunk Nightly/trunk/esales_query_processor/esales_query_processor_ingredient/target/surefire-reports/TEST-com.apptus.esales.FunctionTest.xml
    at hudson.tasks.junit.TestResult.parse(TestResult.java:244)
    at hudson.tasks.junit.TestResult.parse(TestResult.java:163)
    at hudson.maven.reporters.SurefireArchiver.postExecute(SurefireArchiver.java:141)
    at hudson.maven.Maven2Builder.postExecute(Maven2Builder.java:155)
    at hudson.maven.MavenBuilder$Adapter.postExecute(MavenBuilder.java:310)
    at hudson.maven.agent.PluginManagerInterceptor$1MojoIntercepterImpl.callPost(PluginManagerInterceptor.java:170)
    at hudson.maven.agent.PluginManagerInterceptor.executeMojo(PluginManagerInterceptor.java:183)
    … 65 more
    Caused by: 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
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.dom4j.io.SAXReader.read(SAXReader.java:264)
    at hudson.tasks.junit.SuiteResult.parse(SuiteResult.java:112)
    at hudson.tasks.junit.TestResult.parse(TestResult.java:227)
    … 71 more
    Caused by: java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory
    at org.dom4j.DocumentFactory.getInstance(DocumentFactory.java:97)
    at org.dom4j.io.SAXReader.getDocumentFactory(SAXReader.java:645)
    at org.dom4j.io.SAXReader.createContentHandler(SAXReader.java:969)
    at org.dom4j.io.SAXReader.read(SAXReader.java:449)
    … 74 more

  17. David Göransson on Wed, 9th May 2012 4:03 pm
  18. It seems that jenkins has switched its “webroot” from (in my case) the JENKINS_HOME/war to /var/cache/jenkins/war.
    After replacing the dom4j jar in the correct folder, your fix once again works fine!