javaxmlxsdjaxbjaxb2-annotate-plugin

Custom annotation with jaxb2-annotate-plugin and XJC tool


I'm trying to convert bunch of XSD files to Java source POJOs at runtime. These XML schemas will be generated based on some protocol specific model definitions.

I have used the JAXB's XJC to compile the schema to pojo. Started facing problem when I used Jaxb2-annotate-plugin for doing custom annotation.

For testing, I have added following in my schema,

<xs:schema xmlns:aa="http://acme.example.com/system" 
           xmlns:another="http://acme.example.com/another" 
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
           xmlns:ncn="urn:ietf:params:xml:ns:netconf:notification:1.0" 
           jaxb:extensionBindingPrefixes="annox" 
           xmlns:annox="http://annox.dev.java.net"  
           targetNamespace="http://acme.example.com/system" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

XJC started giving the following error from there after,

Unsupported binding namespace "http://annox.dev.java.net". Perhaps you meant "http://java.sun.com/xml/ns/jaxb/xjc"?

As per suggestions provided in various sources, added required JARS in XJC's classpath as follows, but nothing seems to workout for me.

xjc -p com.test -classpath="jaxb2-basics-annotate-1.0.2.jar;tools-0.4.1.5.jar;comm‌​on‌​s-logging-1.1.1.jar;jaxb2-basics-runtime-0.9.5;annox-1.0.1.ja‌​r;javaparser-1.0.11.jar;jaxb2-basics-tools-0.9.5.jar" -extension myschema.xsd

I couldn't find a proper example for generating java sources using XJC with jaxb2-annotate-plugin. I cannot add this in my maven/ant, as I mentioned I've to generate xsd and pojo at runtime.

If using the XJC tool as java process is the only way, please let me know,

If the pojo generation can be done in other ways with this plugin, please suggest the same.

I'm a newbie in jaxb. Any suggestions would be really helpful. Thanks in advance!


Solution

  • Disclaimer: I'm the author of the jaxb2-annotate-plugin.

    First, iff with xjc you mean the binary xjc distributed with JDK (like xjc.exe under Windows), XJC plugins (including jaxb2-annotate-plugin) don't work with xjc binary from JDK.

    The reason is that when packaging XJC for JDK, the XJC packages get renamed from com.sun.tools.xjc to com.sun.tools.internal.xjc. Third-party XJC plugins extend com.sun.tools.xjc.Plugin, not com.sun.tools.internal.xjc.Plugin, this is why they don't work with JDK-packaged XJC.

    Next, you say you generate code in the runtime. I don't quite understand how this is supposed to work, you'll need to compile the classes afterwards etc. But OK, I'll assume you know what you do. Anyway, you should not execute XJC via command line in this case. You have a programmatic API via com.sun.tools.xjc.api.XJC. Check this question and answers:

    Related: