JAXB Bindings to schemas in a JAR

user1234057 picture user1234057 · Feb 29, 2012 · Viewed 10.8k times · Source

I'm using the maven jaxb2 plugin to generate Java classes, built from schemas in a jar. However, I'm not sure how to correctly locate to these schemas from a bindings file. If Iextract the schemas from the jar and drop them in the same directory as the bindings, all is well. However, this isn't a practical long term solution.

pom.xml:

<plugin>
 <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <version>0.8.1</version>
  <executions>
    <execution>
     <goals>
      <goal>generate</goal>
     </goals>
    </execution>
   </executions>
   <configuration>
    <schemas>
     <schema>
      <dependencyResource>
       <groupId>com.test</groupId>
       <artifactId>schemas</artifactId>
       <version>1.10-SNAPSHOT</version>
       <resource>schemas/schema.xsd</resource>
      </dependencyResource>
     </schema>
    </schemas>              
    <bindingDirectory>bindings</bindingDirectory>
    <generatePackage>test.package</generatePackage>
    <bindingIncludes>
     <include>*.xml</include>
    </bindingIncludes>
    <extension>true</extension>
   </configuration>
  </plugin>

bindings.xml:

<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
 xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb ./bindingschema_2_1.xsd"
 version="2.1">

<jxb:bindings schemaLocation="classpath:/schemas/schema.xsd" node="/xs:schema">
  <jxb:bindings node="//xs:complexType[@name='AbstractChangeable']">
   <jxb:class implClass="com.test.AbstractEntity" />
  </jxb:bindings>
</jxb:bindings>

Answer

dma_k picture dma_k · Feb 29, 2012

You need to use maven-dependency-plugin:unpack and then point maven-jaxb2-plugin to outputDirectory. In this case in binding file you need to say something like schemaLocation="../target/schemas/schema.xsd"