Thu 31 Jan 2008
JAXB and XML Schema in Netbeans 6.0 – Customizing
Posted by admin under JAXB , Java , netbeans , programming , xml schema[2] Comments
Problem:Create an XML Schema which will be binded by JAXB to a class structure following certain constrains. You have certain degrees of freedom for your schema design and later you might also use schema annotations provided by JAXB. Modularize your XML schemas, simplest example is: A.xsd uses <include> to include declarations from schema B.xsd.
I want to try Netbeans’ excellent schema design features. Eventually I want to publish the schemas in a WAR deployed in a web container. That’s why I decided to put the files directly into the /web directory of the web-project.
Proposed Solution:
Trying to use Netbeans’ JAXB Wizard (or see http://wiki.netbeans.org/JAXBWizard) to create a JAXB Binding for A.xsd you will meet certain limitations: schema file changes won’t be visible to the “Regenerate” operation. You could edit the copy of A.xsd created by the JAXB Wizard in src/conf/xml-resources/jaxb/name_of_the_binding to make changes visible to the task but that will not see changes in B.xsd. The reason is that the ant tasks created by the JAXB Wizard do not provide this kind of granularity.
So the solution for me was to create the binding using JAXB Wizard, then overriding the created tasks. I copied <target name="jaxb-code-generation" ... from nbproject/xml_binding_build.xml to build.xml and customized the parameters of the XJC task:
<import file="nbproject/build-impl.xml"/> <target name="ask-something"> <input message="Do you like to customize the best IDE of all times?" validargs="Yes, Yes" addproperty="answer"/> <echo message="Answer Yes! ${answer}" /> </target> <target name="jaxb-code-generation" depends="xjc-typedef-target"> <mkdir dir="build/generated/addons/jaxb" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig"/> <mkdir dir="build/generated/jaxbCache" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig"/> <mkdir dir="${build.classes.dir}" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig"/> <mkdir dir="build/generated/jaxbCache/HandleSystemBinding"/> <!-- remove catalog attribut here or change refs in catalog.xml : catalog="catalog.xml" --> <xjc package="de.gbv.examples.handesystembinding" destdir="build/generated/jaxbCache/HandleSystemBinding"> <classpath> <pathelement location="${src.dir}"/> <pathelement path="${jaxbwiz.xjcrun.classpath}"/> </classpath> <arg value="-xmlschema"/> <arg value="-verbose"/> <!-- customize this --> <schema file="web/A.xsd"/> <depends file="web/B.xsd"/> <!-- end customization --> <produces dir="build/generated/jaxbCache/HandleSystemBinding"/> </xjc> <copy todir="build/generated/addons/jaxb"> <fileset dir="build/generated/jaxbCache/HandleSystemBinding"/> </copy> <javac destdir="${build.classes.dir}" srcdir="build/generated/addons/jaxb" source="${javac.source}" target="${javac.target}" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig"> <sourcepath location="${src.dir}"/> <classpath path="${jaxbwiz.gensrc.classpath}"/> </javac> </target>
Note: Later discussions on netbeans issue tracking showed that there is a simpler way by first running “refresh” on the bindings schema and then doing “clean and build”. For that you don’t need to modify the ant target, nevertheless the discussed modification gives you this with just the “Regenerate” action click.
March 16th, 2008 at 2:42 pm
Can you give some links related to the alternative method of running ‘refresh’ and then doing ‘clean and build’?
Thanks.
March 16th, 2008 at 6:30 pm
Voila!
Here the url of the feature request:
http://www.netbeans.org/issues/show_bug.cgi?id=126424