How to execute the JAXB compiler from ANT
Asked Answered
C

2

17

I am using JAXB on a project. the attraction of JAXB is that it is bundled with the JDK, I have been to use xjc.exe on the command line to generate the .java files from a schema. I can't seem to find the JAXB ant task, sure there is a download at http://jaxb.java.net however i want to use the JAXB that is bundled into the JDK is there some way to call JAXB from ant what class does the xjc.exe call on?

Ceja answered 2/9, 2010 at 19:24 Comment(0)
C
20
<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>

Just went hunting in the tools.jar and found the XJCFacade.class in com.sun.tools.internal tested the above code it works it produces the output as xjc.exe It seems that XJC.exe calls this code com.sun.tools.internal.xjc.XJCFacade

One of my key requirements was that the ant file had work within eclipse without having to include a path name to the JDK that way the file would be portable across operating systems. I am assuming that tools.jar is included on the classpath via the installed JRE preferences options.

Ceja answered 2/9, 2010 at 21:10 Comment(1)
For the path to the schema file itself (as well as for the -d attribute), consider using <arg path="..."/> instead of <arg value="..."/> The 'path' version will automatically convert path separators, escape special characters for the shell, etc. Without using path, there's a chance of a command injection vulnerability in your build file if the path or filename is generated from any kind of outside data.Can
S
14

Here is a helpful link:

Java SE 6 does not ship the Ant task (see 7.1.3):

Essentially they do the following:

<target name="xjc" description="....">
    <exec executable="${jdk.dir}/bin/xjc.exe">
        <arg value="-d"/>
        <arg value="${src.dir}"/>
        <arg value="-p"/>
        <arg value="com.mydomain.jaxb"/>
        <arg value="${etc.dir}/myschema.xsd"/>
    </exec>
</target>
Sussi answered 2/9, 2010 at 19:38 Comment(1)
Thanks for our response, I was already familiar with this link. The problem is that this solution requires jaxb-xjc.jar to be present on the classpath and the class that is referenced for the ANT task does not exist any where on the classpath. I don't want to have to download the JAXB2 RI and just to use the ANT task. I am looking for a solution that does not require downloading anything from the net other than latest JDK 6 and that works nicely with eclipse antCeja

© 2022 - 2024 — McMap. All rights reserved.