Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I process multiple xsd schemas using jaxb and the Ant xjc task?

Tags:

jaxb

ant

xsd

xjc

I'm using jaxb to generate java object class from xml schemas within an Ant script like so:

<!-- JAXB compiler task definition -->
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"
                    classpathref="master-classpath"/>

<!-- Generates the source code from the ff.xsd schema using jaxb -->
<target name="option-generate" description="Generates the source code">
    <mkdir dir="${generated-src.dir}/${option.dir}"/>
    <xjc schema="${config.dir}/ff.xsd" destdir="${generated-src.dir}"
         package="${option.package.name}">
        <arg value="-Xcommons-lang" />
        <arg value="-Xcommons-lang:ToStringStyle=SHORT_PREFIX_STYLE" />
        <produces dir="${generated-src.dir}" includes="**/*.java" />
    </xjc>
</target>

Now, this works brilliantly for one schema (ff.xsd in this example). How can I process several schemas (i.e. several xsd files)?

I tried having a separate ant task per schema, but somehow, this doesn't work as Ant process the first task and then says that the "files are up to date" for the following schemas!

like image 432
dm76 Avatar asked Dec 17 '09 21:12

dm76


2 Answers

<target name="process-resources" description="Process resources">
    <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"/>
    <xjc destdir="${basedir}/target/generated-sources/jaxb"
         extension="true">
        <schema dir="src/main/xsd" 
                includes="JaxbBindings.xsd,CoreTypes.xsd"/>
    </xjc>
</target>
like image 53
maximdim Avatar answered Sep 20 '22 13:09

maximdim


<target name="generate-jaxb-code">
    <java classname="com.sun.tools.internal.xjc.XJCFacade">
            <arg value="-p" />
            <arg value="com.example"/>
            <arg value="xsd/sample.xsd" />
    </java>
</target>

works with the JAXB that is part of JDK 6 seems that the ANT task only ships with the downloadable JAXB but since JAXB is part of the JDK its probably not a good idea to take the latest release of the JAXB and add to the classpath of the JDK since that means you probably need to mess around with classloader settings, to pickup the downloaded version rather than the version within the JDK.

like image 39
ams Avatar answered Sep 18 '22 13:09

ams