Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Bindings to schemas in a JAR

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>
like image 392
user1234057 Avatar asked Feb 29 '12 21:02

user1234057


People also ask

What is JAXB binding file?

JAXB is an XML-to-Java binding technology that enables transformation between schema and Java objects and between XML instance documents and Java object instances. JAXB technology consists of a runtime API and accompanying tools that simplify access to XML documents.

What is XJB file in JAXB?

xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

How use XJC command line?

Open a command prompt. Run the JAXB schema compiler, xjc command from the directory where the schema file is located. The xjc schema compiler tool is located in the app_server_root \bin\ directory. Use the generated JAXB objects within a Java application to manipulate XML content through the generated JAXB classes.

Which command line command is used to generate XML schemas from Java source files?

You can create an XML schema document from an existing Java application that represents the data elements of a Java application by using the JAXB schema generator, schemagen command-line tool.


2 Answers

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"

like image 147
dma_k Avatar answered Sep 18 '22 00:09

dma_k


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.

like image 25
lexicore Avatar answered Sep 20 '22 00:09

lexicore