Java Reference
In-Depth Information
Generating Java Source Files from XML Schema in
Ant
Problem
You want to invoke XJC from an Ant script.
Solution
Use the Ant task for XJC, available in sun-appserv-ant.jar.
Discussion
In Generating Java Classes from Schema , you saw how to use XJC from the command line to
generate Java source files based on XML schemas. But that's not a very real-world way to do
things. Ideally, you could change your schemas at will and have the Java sources regenerate
when you build your service. If you're using Ant to do your building, this is a breeze.
In your build script, you'll be defining the Sun XJC wrapper task, but you need a few JARs
from Glassfish on your classpath first. Add these entries to your build script's properties file:
sun.app.ant.jar=${glassfish.jars.dir}/sun-appserv-ant.jar
sun.ws.tools.jar=${glassfish.jars.dir}/webservices-tools.jar
sun.ws.rt.jar=${glassfish.jars.dir}/jaxws-rt.jar
xjc.task.path=${sun.app.ant.jar}${path.separator}${sun.ws.tools.jar}${path.separator}
${sun.ws.rt.jar}
This will allow Ant to find XJC and the Ant wrapper task. Now in your build.xml, define the
following:
<taskdef name="xjc"
classname="com.sun.tools.xjc.XJCTask">
<classpath path="${xjc.task.path}"/>
</taskdef>
Then invoke the task before compiling your service code:
<target name="schema-to-java">
<echo message="-----Generating Java sources from Schema-----" />
<xjc destdir="./src/gen">
<schema dir="./src/xml/META-INF/wsdl" includes="**/*.schemalet, **/
*.xsd"/>
Search WWH ::




Custom Search