dm76
dm76

Reputation: 4260

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

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!

Upvotes: 10

Views: 20724

Answers (3)

CodeMed
CodeMed

Reputation: 9201

You can also just include the other xsd files in your main xsd file, using a command like the following:

    <xs:include schemaLocation="path/to/secondschema.xsd"/>

Upvotes: 0

maximdim
maximdim

Reputation: 8169

<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>

Upvotes: 11

ams
ams

Reputation: 62722

<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.

Upvotes: 3

Related Questions