Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate an XML schema file from (JAXB) annotated Java classes, using JDK8

Tags:

java-8

jaxb

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?

like image 678
ali65 Avatar asked Mar 20 '14 16:03

ali65


People also ask

Which component or tool in JAXB can generate Java files from schemas?

After the Java artifacts for your application are generated, you can generate fully annotated Java classes from an XML schema file by using the JAXB schema compiler, xjc command-line tool.

How do you create a schema for a class in Java?

In the main menu, go to Tools | XML Actions | Generate XML Schema From Java Using JAXB. In the dialog that opens, specify the method parameter and return types to be reflected in the generated Schema: To have all the class methods involved, clear the Include parameter and return type of the following methods checkbox.

How do you create a Java class from an XML schema?

Generate a Java class from an XML Schema using JAXB In the active editor tab, open the desired Schema . xsd file or an XML document, which contains the desired Schema. In the main menu, go to Tools | XML Actions | Generate Java Code From XML Schema Using JAXB.

How do I generate Java classes from XSD using JAXB maven?

Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory.


1 Answers

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>
like image 186
Marco Santos Avatar answered Oct 07 '22 20:10

Marco Santos