mojohaus / jaxb2-maven-plugin

JAXB2 Maven Plugin
https://www.mojohaus.org/jaxb2-maven-plugin/
Apache License 2.0
106 stars 77 forks source link

Problem running schemagen with JDK 1.8 when createJavaDocAnnotations is true #40

Closed netzling closed 8 years ago

netzling commented 8 years ago

When running schemagen from the command line (the 1.8 version) everything is fine, a schema is generated as expected. When the jaxb2-maven-plugin runs schemagen, it fails with a report that says that schemagen did not finish properly. Running maven with the -e flag I get the following cause:

Caused by: java.lang.IllegalArgumentException: Not supported: indent-number
    at org.apache.xalan.processor.TransformerFactoryImpl.setAttribute(TransformerFactoryImpl.java:571)
    at org.codehaus.mojo.jaxb2.schemageneration.XsdGeneratorHelper.getFactory(XsdGeneratorHelper.java:544)
    at org.codehaus.mojo.jaxb2.schemageneration.XsdGeneratorHelper.getHumanReadableXml(XsdGeneratorHelper.java:440)
    at org.codehaus.mojo.jaxb2.schemageneration.XsdGeneratorHelper.savePrettyPrintedDocument(XsdGeneratorHelper.java:508)
    at org.codehaus.mojo.jaxb2.schemageneration.XsdGeneratorHelper.insertJavaDocAsAnnotations(XsdGeneratorHelper.java:233)
    at org.codehaus.mojo.jaxb2.schemageneration.AbstractXsdGeneratorMojo.performExecution(AbstractXsdGeneratorMojo.java:454)
    ... 22 more

POM sets java to 1.8:

                    <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>3.3</version>
                            <configuration>
                                    <source>1.8</source>
                                    <target>1.8</target>
                            </configuration>
                    </plugin>

The plugin is run like so:

                    <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>jaxb2-maven-plugin</artifactId>
                            <version>2.2</version>
                            <configuration>
                                    <sources>
                                            <source>src/main/java/testdata</source>
                                    </sources>
                            </configuration>
                            <executions>
                                    <execution>
                                            <id>schemagen</id>
                                            <goals>
                                                    <goal>schemagen</goal>
                                            </goals>
                                    </execution>
                            </executions>
                            <!-- Use default configuration, implying that sources are read from the 
                                    directory src/main/java below the project basedir. (i.e. getProject().getCompileSourceRoots() 
                                    in Maven-speak). -->
                    </plugin>                       

Output with mvn -X -e compile:

Apache Maven 3.0.5 (Red Hat 3.0.5-16)
Maven home: /usr/share/maven
Java version: 1.8.0_65, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-2.b17.el7_1.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-229.20.1.el7.x86_64", arch: "amd64", family: "unix"
[INFO] Error stacktraces are turned on.
[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml

...

Will attach the complete log.

netzling commented 8 years ago

Complete log for running maven with mvn -X -e compile: log.txt

netzling commented 8 years ago

It appears that indent-number is only accepted by Xalan in JDK 1.7 and 1.6, see http://stackoverflow.com/questions/15134861/java-lang-illegalargumentexception-not-supported-indent-number.

netzling commented 8 years ago

Setting the configuration property createJavaDocAnnotations to false avoids the problem, but is, of course, only a workaround.

lennartj commented 8 years ago

I suppose we can handle this in the plugin by checking the JDK version, and applying the correct setting. If possible, we would need the same functionality to create human-readable comments (i.e. with indentation) even under JDK 8.

cyberoblivion commented 8 years ago

This issue also appears if you have transformSchemas, even if you turn off createJavaDocAnnotations, you will get this exception until you remove all transformSchemas.

lennartj commented 8 years ago

The error is thrown from the following code snippet:

    private static TransformerFactory getFactory() {

        if (FACTORY == null) {

            try {
                FACTORY = TransformerFactory.newInstance();

                // Harmonize XML formatting
                FACTORY.setAttribute("indent-number", 2);

            } catch (Throwable exception) {

                // This should really not happen... but it seems to happen in some test cases.
                throw new IllegalStateException("Could not acquire TransformerFactory implementation.", exception);
            }
        }

        // All done.
        return FACTORY;
    }

... where the desired outcome is to provide a more sensible XSD formatting than if the indent-number argument was not provided. The "indent-number" parameter, in turn is found - along with the other accepted parameters - in the implementation of the TransformerFactory. The implementation found within the JDK8 (com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl) does contain the constant - so I believe that you are using a TransformerFactory other than the JDK8 internal one.

However, a simple way to handle IllegalArgumentException is simply to catch it and move on. In case the TransformerFactory implementation does not support the indent-number attribute, the user will simply get a less well-formatted, generated XSD as a result.