Maven, Jaxb-Schemagen Plugin POM.xml Setup and How to Generate XML Schemas for POJOs

When ever I need to use the JAXB maven plugin, it is always a task to configure the maven pom.xml file to get this tool to run, but usually an answer can be found pretty quickly so its just annoying. Most often I just use javaws and jaxb plugins to import and export web services, but recently I had to export XML schemas for some POJOs for a client, and this was a bit more of a challenge. It seems, from the google search results at least, that Java developers do not export XML schema definitons for POJO's that often.

JAXB-Schemagen, Maven Plugin for Java To XML Schemas - Build Errors

The  amount of information, or dearth thereof, on the home pages for the two plugins also highlights this anomaly

  • XML to Java and back (maven-jaxb2 ) -well explained and documented
  • Java to XML schema (jaxb-schemagen ) - barest minimum documentation

I setup my pom.xml, as describe on the plugin home page, to use the jaxb-schemagen plugin but just kept getting the following errors:

[WARNING] POM for 'com.sun.xml.bind:jaxb-xjc:pom:2.0.2:runtime' is invalid. It will be ignored for artifact resolution. 
Reason: Not a v4.0.0 POM. for project com.sun.xml.bind:jaxb-xjc at 
/home/mark/.m2/repository/com/sun/xml/bind/jaxb-xjc/2.0.2/jaxb-xjc-2.0.2.pom

 

[INFO] [jaxb-schemagen:generate {execution: default}]

 

[FATAL ERROR] com.sun.tools.jxc.maven2.SchemaGenMojo#execute() caused a linkage error
(java.lang.NoClassDefFoundError) and may be out-of-date. Check the realms:
[FATAL ERROR] Plugin realm = app0.child-container[com.sun.tools.jxc.maven2:maven-jaxb-schemagen-plugin]

[INFO] com/sun/tools/jxc/AptBasedTask
com.sun.tools.jxc.AptBasedTask
[INFO] ------------------------------------------------------------------------
[INFO] Trace
java.lang.NoClassDefFoundError: com/sun/tools/jxc/AptBasedTask

 

 

Correct POM.XML Configurationof Jaxb-SchemaGen

After trying various false leads, like thinking jxc was a mispelling of xjc, trying to find out why Maven couldn't find tools.jar in the JAVA_HOME directory etc, I finally worked out that the Jaxb-schemagen plugin's pom.xml was including older versions of the bind-xjc and jaxb-xjc jar files. What was fustrating was that I was attempting to override the default versions of these jar files, defined in the plugin's pom.xml, in the dependencies stanza of the pom.xml, but this seemed to have no effect. After several hours of fustration I  managed to get it working once I discovered that, to override plugin dependencies, the plugin definition needs to include its own dependencies stanza.

Here is the working pom.xml


       <plugin>
            <groupId>com.sun.tools.jxc.maven2</groupId>
            <artifactId>maven-jaxb-schemagen-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                     <goals>
                        <goal>generate</goal>
                     </goals>
               </execution>
           </executions>

            <configuration>
                <project>${project}</project>
                <destdir>${project.build.directory}/schemas</destdir>
                <srcdir>${project.build.sourceDirectory}/za/co/jumpingbean/dal/model/core/</srcdir>
                <verbose>true</verbose>
            </configuration>
           <dependencies>
                <dependency>
                    <groupId>javax.xml.bind</groupId>
                    <artifactId>jaxb-api</artifactId>
                    <version>2.2</version>
                </dependency>
                <dependency>
                    <groupId>com.sun.xml.bind</groupId>
                    <artifactId>jaxb-xjc</artifactId>
                    <version>2.2</version>
                </dependency>
                <dependency>
                    <groupId>com.sun.xml.bind</groupId>
                    <artifactId>jaxb-impl</artifactId>
                    <version>2.2</version>
                </dependency>
                <dependency>
                    <groupId>com.sun.xml.bind</groupId>
                    <artifactId>jaxb-xjc</artifactId>
                    <version>2.2</version>
                </dependency>
           </dependencies>
      </plugin>

 

You may need to define a plugin repository too:

     
    <pluginRepositories>
    <pluginRepository>
            <id>maven-repository.dev.java.net</id>
            <name>Java.net Maven 2 Repository</name>
            <url>http://download.java.net/maven/2</url>
    </pluginRepository>
  </pluginRepositories>   
  

 

Comments

Am trying to come up with XSD from java code.
from the command prompt using schemagen it works perfect, however when i do it with maven , it gives back the following error.

Thanks for looking at this.

Problem encountered during annotation processing;
see stacktrace below for more information.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.tools.jxc.apt.InlineAnnotationReaderImpl.getClassValue(InlineAnnotationReaderImpl.java:150)
at com.sun.tools.jxc.apt.InlineAnnotationReaderImpl.getClassValue(InlineAnnotationReaderImpl.java:64)
at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl.getTypes(ElementPropertyInfoImpl.java:137)
at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.size(ElementPropertyInfoImpl.java:78)
at java.util.AbstractList$Itr.hasNext(AbstractList.java:339)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:255)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:209)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:315)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:330)
at com.sun.tools.xjc.api.impl.j2s.JavaCompilerImpl.bind(JavaCompilerImpl.java:90)
at com.sun.tools.jxc.apt.SchemaGenerator$1.process(SchemaGenerator.java:115)
at com.sun.mirror.apt.AnnotationProcessors$CompositeAnnotationProcessor.process(AnnotationProcessors.java:60)
at com.sun.tools.apt.comp.Apt.main(Apt.java:454)
at com.sun.tools.apt.main.JavaCompiler.compile(JavaCompiler.java:258)
at com.sun.tools.apt.main.Main.compile(Main.java:1102)
at com.sun.tools.apt.main.Main.compile(Main.java:964)
at com.sun.tools.apt.Main.processing(Main.java:95)
at com.sun.tools.apt.Main.process(Main.java:85)
at com.sun.tools.apt.Main.process(Main.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.tools.jxc.AptBasedTask$InternalAptAdapter.execute(AptBasedTask.java:97)
at com.sun.tools.jxc.AptBasedTask.compile(AptBasedTask.java:144)
at com.sun.tools.jxc.maven2.SchemaGenAdapter.execute(SchemaGenAdapter.java:111)
at com.sun.tools.jxc.maven2.SchemaGenMojo.execute(SchemaGenMojo.java:161)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:453)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:559)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:500)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:479)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:331)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:292)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:142)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:336)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:129)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:301)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.tools.jxc.apt.InlineAnnotationReaderImpl.getClassValue(InlineAnnotationReaderImpl.java:139)
... 44 more
Caused by: java.lang.annotation.AnnotationTypeMismatchException: Incorrectly typed data found for annotation element public abstract java.lang.Class javax.xml.b
ind.annotation.XmlElement.type() (Found data of type java.lang.Class)
at com.sun.tools.apt.mirror.declaration.AnnotationProxyMaker$ValueVisitor$1.generateException(AnnotationProxyMaker.java:241)
at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:56)
at $Proxy9.type(Unknown Source)
at com.sun.xml.bind.v2.model.annotation.XmlElementQuick.type(XmlElementQuick.java:36)
... 49 more