3
votes

In our maven project, we have a folder that holds several xsd and xjb files. So we apply the JAXB customization using maven-jaxb2-plugin.

Currently, we have two xjb files applied to all xsd 1)GlobalBindings.xjb 2)ServiceSpecific.xjb. ServiceSpecific.xjb is more of finer controlled binding at some element specific.

Below is the GlobalBindings.xjb

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
      xmlns:annox="http://annox.dev.java.net"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      jaxb:version="2.0"
      jaxb:extensionBindingPrefixes="xjc annox">

    <jaxb:globalBindings>
        <xjc:javaType name="java.util.Date" xmlType="xs:dateTime"  adapter="company.jaxb.adapter.DateTimeAdapter"/>
    </jaxb:globalBindings>
</jaxb:bindings>

With this approach, I get @XmlJavaTypeAdapter(DateTimeAdapter.class) annotation on our xs:dataTime fields in the generated objects.

However, there are some schemas, whose JAXB objects need to have a different version of DateTimeAdapter.class, say DateTimeAdapter1.class.

I'm planning to achieve this, by separating out those specific xsd into a separate folder and pairing it with those specific xjb, which provides different DateTimeAdapter1 in maven plugin through a new exection step. something like below

<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
 <executions>
   <execution>
     <id>specific</id>
      <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
       <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory
       <bindingDirectory>${basedir}/src/main/resources/xsd</bindingDirectory>
        <generatePackage>company.service.generated</generatePackage>
        ......................

         ......................     
         <includeSchemas>
           <includeSchema>**/specific.xsd</includeSchema>
         </includeSchemas>

But wondering, without going through this hassle, if there is a better way of customizing at individual schema level in servicespecific.xjb , something like

<jaxb:bindings schemaLocation="specific.xsd" multiple="true">
        <jaxb:bindings node="xs:complexType/xs:sequence/xs:element[@type='xs:dateTime']"
                            multiple="true">

            <jaxb:globalBindings>
                <xjc:javaType name="java.util.Date" xmlType="xs:dateTime"  adapter="company.jaxb.adapter.DateTimeAdapter1"/>
            </jaxb:globalBindings>
        </jaxb:bindings>
    </jaxb:bindings>

I tried the above approach, but while generating, maven fails with error message

"compiler was unable to honor this globalBindings customization. It is attached to a wrong place, or its inconsistent with other bindings."

So, apparently what I tried to do here is incorrect. What are my alternatives apart from change in pom (i.e using different execution on set of files, which takes lot of pain to rearrange the XSD)

Thanks

1

1 Answers

1
votes

Author of here.

I think there's no really better way. Global bindings are global. So if you want different global bindings on different schema subsets, you'll have to compile them separately. Just as you do.

You don't have to rearrange the schemas though. You can configure different schema directories or patterns or individual files.