JAXB Bindings to schemas in a JAR

10,957

Solution 1

What I'd like to have working here is something like:

<jaxb:bindings schemaLocation="maven:org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-tests-po!/purchaseorder.xsd" node="/xs:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="org.jvnet.jaxb2.maven2.tests.po"/>
    </jaxb:schemaBindings>      
</jaxb:bindings>

But it does not at the moment. Please file an issue, I'll try to fix it.

What does work now is SCD-based binding:

<jaxb:bindings scd="x-schema::po" xmlns:po="urn:po">
    <jaxb:schemaBindings>
        <jaxb:package name="org.jvnet.jaxb2.maven2.tests.po"/>
    </jaxb:schemaBindings>      
</jaxb:bindings>

So you don't actually need to bind based on a specific schema location, you can bind based on the namespace URI, which is theoretically better.

Practically I have an experience that SCD-bindings don't always work reliably.

UPDATE

See this link for more information SCD usage in JAXB.

Solution 2

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"

Share:
10,957
user1234057
Author by

user1234057

Updated on June 23, 2022

Comments

  • user1234057
    user1234057 almost 2 years

    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>
    
  • user1234057
    user1234057 about 12 years
    I've gotten that type of solution working already as mentioned, but I'd like to know how to reference schemas inside of a jar.
  • user1234057
    user1234057 about 12 years
    I'm not really sure how to use this SCB binding technique to refer to schemas inside of a maven provided dependency jar, are there examples?
  • lexicore
    lexicore about 12 years
    I have posted the example above. You have to specify the namespace prefix (xmlns:po="urn:po") and use this prefix in the scd expression (scd="x-schema::po" means "address the schema with the target namespace from prefix po"). See this link: jaxb.java.net/guide/Using_SCD_for_customizations.html
  • user1234057
    user1234057 about 12 years
    We didn't really understand how to reference a maven jar with that method, but we'll wait until the plugin is updated. Thanks for the help!
  • lexicore
    lexicore about 12 years
    You don't need to reference a maven jar with this method. That is, you don't say "I refer to this element from this schema from this JAR". Instead you do binding via namespace, which is purely logical. What you express with SCD is "I refer to an element of the schema with the given namespace". No matter where the schema comes from. And please file an issue, otherwise I won't know who an why is interested in this functionality.
  • gknicker
    gknicker about 9 years
    Your answer would be much better with a code example.
  • Superaghu
    Superaghu over 5 years
    This does not work as unpack is tied to generate-resources goal and xjc runs in generate-sources goal which is earlier step
  • Superaghu
    Superaghu over 5 years
    has this issue been fixed in the plugin - if you do not want to go the SCD based approach?
  • dma_k
    dma_k over 5 years
    You could bind unpack to generate-sources phase (or earlier) as well. Any difficulties?
  • lexicore
    lexicore over 5 years
    @Superaghu I don't remember fixing it.
  • Superaghu
    Superaghu over 5 years
    Thanks @dma_k - I bound it to initialize phase and able to get it to working