The Source for Java Technology Collaboration
User: Password:



Kohsuke Kawaguchi

Kohsuke Kawaguchi's Blog

Using Maven JAXB2 plugin to compile DTD

Posted by kohsuke on January 30, 2007 at 09:44 AM | Comments (6)

Glassfish v3 is going to be based on Maven2, and I've been helping them to get it right. One of the work is to invoke JAXB's schema compiler to compile a DTD with XJC plugins. Once you know what to do, this is easy thanks to the excellent maven-jaxb2-plugin.

As with everything else in Maven, the hard part is to know how to do it. I thought this example might be useful for other folks, so decided to post it here. The crucial portion is the plugin configuration in <build>:

<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <!--  if you want to put DTD somewhere else
        <schemaDirectory>src/main/jaxb</schemaDirectory>
        -->
        <extension>true</extension>
        <schemaLanguage>DTD</schemaLanguage>
        <schemaIncludes>
          <schemaInclude>*.dtd</schemaInclude>
        </schemaIncludes>
        <bindingIncludes>
          <bindingInclude>*.jaxb</bindingInclude>
        </bindingIncludes>
        <args>
          <arg>-Xinject-listener-code</arg>
        </args>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>org.jvnet.jaxb2-commons</groupId>
      <artifactId>property-listener-injector</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>
</plugin>

The dependencies section inside the plugin element can be used to specify additional XJC plugins. If you'd like to use more recent version of the JAXB RI, you can specify a dependency to XJC here to do so, like this:

<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-xjc</artifactId>
  <version>2.1.2</version>
</dependency>

The complete sample project is also available.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • The downside of all the jaxb plugins I found was their lack of support for running with JDK6. Suppose I want to use the jaxb-api-2.1 and/or jaxws-api-2.1 jars. In this case we must use endorsed standards override - something the jaxb plugins don't support.
    The workaround is to lauch xjc or wsimport with the maven-java-plugin instead. The size of the configuration is about the same and the interface is tightly bound to the tools.

    Posted by: plynch on January 31, 2007 at 07:03 AM

  • Hmm. True. I wonder if ther's any classloader trick that we can play to get around the issue.


    In the mean time, you can also use JAXB RI 2.0.5.

    Posted by: kohsuke on January 31, 2007 at 07:48 PM

  • Glassfish v3 based on maven2, now that's great news, I'd love to see more sun projects based on maven2!

    Posted by: francisdb on February 01, 2007 at 01:00 AM

  • I tried this and was pleasantly surprised by the ease with which it all worked. It only took about half an hour from dtd to working code.
    I have one problem left though.
    In the DTD the following construct is used to define an OnlineResource element


    <!ELEMENT OnlineResource EMPTY>
    <!ATTLIST OnlineResource
    xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink"
    xlink:type CDATA #FIXED "simple"
    xlink:href CDATA #REQUIRED >

    This aims to define a prefix xlink for the "http://www.w3.org/1999/xlink" namespace and subsequently use that for the xlink:href attribute.
    When I use the 2.1.6 version of jaxb to generate code the OnlineResource class looks like this

    public class OnlineResource {

    @XmlAttribute(name = "xmlns:xlink")
    @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
    protected String xmlnsXlink;
    @XmlAttribute(name = "xlink:type")
    @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
    protected String xlinkType;
    @XmlAttribute(name = "xlink:href", required = true)
    @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
    protected String xlinkHref;


    When I unmarshall a sample xml with an OnlineResource element like this

    <HTTP>
    <Get>
    <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
    xlink:type="simple"
    xlink:href="http://www.cubewerx.com/demo/cubeserv/cubeserv.cgi?"/>
    </Get>
    </HTTP>

    all attributes of the OnlineResource object remain null.
    I have been looking around and found that this is a valid construct for using namespaces in a DTD.
    Does JAXB currently support this?

    Posted by: wberkel on April 14, 2008 at 07:46 AM

  • wberkel — I added a page in the JAXB user guide to discuss this briefly.

    Posted by: kohsuke on April 16, 2008 at 11:48 AM

  • Thanks

    Posted by: wberkel on April 16, 2008 at 12:31 PM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds