1. Mixing JDOM 1 and 2 in a Maven project

    This article describes a question I asked on Stack Overflow. I'm including it here to keep all my technical articles in a single spot.

    In a Maven based project some of the JUnit tests failed during a Maven site build. When executing mvn clean package all tests pass. But when executing mvn clean site some tests produce errors:

    Could not initialize class org.jdom2.input.sax.XMLReaders

    These errors occur in a class that uses ROME to parse RSS data. The errors did not happen when the ROME dependency was not yet included in the project so I expect that is the cause. ROME has a dependency to JDOM 2.0.2.

    My project includes the cobertura-maven-plugin to generate a test coverage report and this plugin also has a dependency to JDOM. It uses Jaxen, which needs JDOM 1.0

    My guess is that while executing the site goal, the Cobertura plugin is active and the JDOM version 1.0 is used by the class under test. This causes the errors in the ROME library because of the incorrect JDOM version.

    The problem was solved by setting a system value during program startup:

    System.setProperty("javax.xml.parsers.SAXParserFactory",
        "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");

    I found this solution in this post: JDOM2: This parser does not support specification "null" version "null".