5

The instructions to generate schema through running "schenagen" as suggested in Java API documentation worked with JDK7, but not with JDK8.

Here is the documentation page: http://download.java.net/jdk8/docs/technotes/guides/xml/jaxb/index.html

Here is the line from this page with links to the instructions:

" Running the schema generator (schemagen): [command-line instructions, using the SchemaGen Ant task] "

Schema generator does not work because some classes have been removed from JDK8: "java.lang.ClassNotFoundException: com.sun.mirror.apt.AnnotationProcessorFactory"

There is another solution suggested here: Generating XSD schemas from JAXB types in Maven?

This solution also works with JDK7 but not with JDK8; it will end up with a similar error:

"Class not foundcom/sun/tools/apt/Main.class"

The root cause is probably the same: the annotation processing tools are removed from JDK8. This change was planned in JEP 117 long time ago: http://openjdk.java.net/jeps/117

How can I generate an XML schema file from (JAXB) annotated Java classes now, using JDK8?

Community
  • 1
  • 1
ali65
  • 106
  • 1
  • 6

2 Answers2

9

This was a bug in the "jaxb2-maven-plugin". You must use the version 1.6 or later of the plugin

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>schemagen</goal>
                    </goals>
                    <phase>generate-resources</phase>
                    <configuration>
                        <includes>
                            <include>com/projectname/model/*.java</include>
                        </includes>
                        <outputDirectory>${project.build.directory}/schemas</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Marco Santos
  • 91
  • 1
  • 2
  • Using version 2.2 I could make schemagen is working. The suggested 1.6 version printed message that sources are not changed, skipping schema generation. – ali65 Sep 21 '15 at 22:04
  • Yes , using 1.6 worked. ( Seems 1.5 is not working with Java 8 ).Thank you. – Buminda Nov 24 '16 at 00:14
0

You can use the generateSchema method on JAXBContext to generate an XML Schema:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    This path looks promising even with JDK8: http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html . Any suggestions how to call this method in a Maven pom file? Is there a Maven plugin which is based on calling this method and not using APT? – ali65 Mar 20 '14 at 16:53