How to generate *.xjb file from WSDL? - jaxb

I have a WSDL file weatherservice.wsdl and I am trying to generate binding for this WSDL using xjc. How do I do this using xjc?
I did not find any command line args to do it from xjc. xjc -p com -wsdl weatherservice.wsdl

Generally we create a bindings file with .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.
Here is an example:
<jaxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc"
version="2.1">
<jaxb:globalBindings generateIsSetMethod="true" fixedAttributeAsConstantProperty="true">
<xjc:serializable uid="1" />
</jaxb:globalBindings>
<jaxb:bindings schemaLocation="abcd.xsd">
<jaxb:bindings node="//xs:element[#name='Event']/xs:simpleType">
<jaxb:typesafeEnumClass name="EventEnumType" />
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>

Try using this simple binding. Save it next to the WSDL and tell XJC to use that.
<bindings version="2.0"
xmlns="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"
>
<globalBindings>
<xjc:simple/>
</globalBindings>
</bindings>

You could do it through the Maven plugin, which simplifies a bit, and complicates a bit.
This will take a few XSDs and apply the XJBs to generate the Java classes:
<!-- Docs:
Maven plugin: http://cxf.apache.org/cxf-xjc-plugin.html
XJC: https://javaee.github.io/jaxb-v2/doc/user-guide/ch04.html
https://tech.boldare.com/make-jaxb-great-again/
-->
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<version>${version.cxf-xjc}</version>
<configuration>
<sourceRoot>${basedir}/target/generated-sources/main/java</sourceRoot>
<xsdOptions>
<xsdOption>
<extension>true</extension>
<xsd>${xsdsBaseDir}/someService/some-soap-service.xsd</xsd>
<!-- Args to XJC execution -->
<extensionArgs>
<extensionArg>-XautoNameResolution</extensionArg>
<extensionArg>-encoding</extensionArg><extensionArg>UTF-8</extensionArg>
</extensionArgs>
</xsdOption>
<xsdOption>
<!-- another XSD... -->
</xsdOption>
</xsdOptions>
<extensions>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-wsdlextension:${version.cxf-xjc}</extension>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-boolean:${version.cxf-xjc}</extension>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-ts:${version.cxf-xjc}</extension>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:${version.cxf-xjc}</extension>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-bug671:${version.cxf-xjc}</extension>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-bug986:${version.cxf-xjc}</extension>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-javadoc:${version.cxf-xjc}</extension>
<!--
-->
</extensions>
</configuration>
<executions>
<execution>
<id>generate-from-xsds</id><phase>generate-sources</phase><goals><goal>xsdtojava</goal></goals>
</execution>
</executions>
</plugin>
Two gotchas:
The plugin can't take defaults from the execution config - must be set in the plugin-level config.
You'll need to add the sources to the reactor, if you have some tests of the schema within the same module.

Related

jaxb2 simplify plugin binding issue

I tried to use JAXB2 simplify plugin using the XJC 2.2.4.
D:\>xjc -d . -extension -p org.my.space sample.xsd parsing a schema... [ERROR] Unsupported binding namespace
"http://jaxb2-commons.dev.java.net/basic/simplify". Perhaps you meant
"http://jaxb.dev.java.net/plugin/code-injector"? line 7 of file:/D:/sample.xsd
Failed to parse a schema.
sample.xsd has the declared simplify
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify"
jaxb:extensionBindingPrefixes="simplify"
xmlns="http://www.example.org/sampleXML" targetNamespace="http://www.example.org/sampleXML"
elementFormDefault="qualified">
any thoughts?
EDIT:
I tried other way around with bindings file xjb and still the same error. Does it mean my XJC version(2.2) does not support JAXB2 simplify ?
xjc -d . -extension -b bindings.xjb sample.xsd
<bindings version="2.1"
xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify"
extensionBindingPrefixes="simplify">
<bindings schemaLocation="sample.xsd" node="/xs:schema">
<bindings
node="/xs:schema/xs:complexType[#name='dlist']/xs:choice/xs:element[1]">
<simplify:as-element-property />
</bindings>
</bindings>
</bindings>
I switched to maven jaxb2 plugin and with some more efforts it worked fine. For people who have trouble in future. This setup works just fine in JDK 1.6 (for those still cant migrate).
pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- plugin to compile schemas to JAXB classes -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb21-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<id>generate-JAXB</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<forceRegenerate>true</forceRegenerate>
<schemaDirectory>src/main/resources</schemaDirectory>
<schemaIncludes>
<include>sample.xsd</include>
</schemaIncludes>
<cleanPackageDirectories>true</cleanPackageDirectories>
</configuration>
</execution>
</executions>
<configuration>
<extension>true</extension>
<args>
<arg>-Xsimplify</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.11.0</version>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</build>
I did not use binding.xjb as I have included the simplify directly in the schema (as i have access).
sample.xsd
<xs:complexType name="doclist">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property />
</xs:appinfo>
</xs:annotation>
<xs:element name="document1" type="type1" />
<xs:element name="document2" type="type2" />
</xs:choice>
</xs:sequence>
<xs:attribute name="heading" type="xs:string" />
</xs:complexType>

xjc / jaxb2-commons interface binding issue, customization attached to the wrong place

I'm trying to add an interface using the jaxb2-basics artifact from the jaxb2_commons maven group.
My pom.xml contains the following dependencies
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>0.6.4</version>
</dependency>
and the plugin configuration looks like
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb22-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>jaxb-generate-messages-in</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<specVersion>2.2</specVersion>
<schemaLanguage>XMLSCHEMA</schemaLanguage>
<schemaDirectory>src/main/schema</schemaDirectory>
<schemaIncludes>
<include>MESSAGES-IN.xsd</include>
</schemaIncludes>
<bindingDirectory>src/main/binding</bindingDirectory>
<bindingIncludes>
<include>messages-in-binding.xjb</include>
</bindingIncludes>
<episodeFile>${project.build.directory}/generated-sources/messages-in/META-INF/jaxb-messages-in.episode</episodeFile>
<generateDirectory>${project.build.directory}/generated-sources/messages-in</generateDirectory>
<extension>true</extension>
<args>
<arg>-Xinheritance</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</plugin>
</plugins>
</configuration>
</execution>
<execution>
<id>jaxb-generate-messages-out</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<specVersion>2.2</specVersion>
<schemaLanguage>XMLSCHEMA</schemaLanguage>
<schemaDirectory>src/main/schema</schemaDirectory>
<schemaIncludes>
<include>MESSAGES-OUT.xsd</include>
</schemaIncludes>
<bindingDirectory>src/main/binding</bindingDirectory>
<bindingIncludes>
<include>messages-out-binding.xjb</include>
</bindingIncludes>
<episodeFile>${project.build.directory}/generated-sources/messages-out/META-INF/jaxb-messages-out.episode</episodeFile>
<generateDirectory>${project.build.directory}/generated-sources/messages-out</generateDirectory>
</configuration>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
</configuration>
</plugin>
As you can see from the above, there are two invocations of xjc, which both work. Focusing on the first one, my bindings file
<jaxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jaxb:extensionBindingPrefixes="xjc"
version="1.0">
<jaxb:bindings schemaLocation="../schema/MESSAGES-IN.xsd" node="/xs:schema">
<jaxb:globalBindings typesafeEnumMaxMembers="3000">
<jaxb:serializable uid="1"/>
</jaxb:globalBindings>
<jaxb:schemaBindings>
<jaxb:package name="com.whatever.messages.request"/>
</jaxb:schemaBindings>
<jaxb:bindings node="//xs:simpleType[#name='YesOrNo']">
<jaxb:class ref="com.whatever.messages.YesOrNo"/>
</jaxb:bindings>
<jaxb:bindings noade="//xs:complexType[#name='someLogin']">
<jaxb:class name="LoginRequest">
<jaxb:javadoc><![CDATA[A Login request message.]]>
</jaxb:javadoc>
</jaxb:class>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
works like a charm; but, when I attempt to add an interface to 'LoginRequest'...
<jaxb:bindings node="//xs:complexType[#name='someLogin']">
<jaxb:class name="LoginRequest">
<jaxb:javadoc><![CDATA[A Login request message.]]>
</jaxb:javadoc>
</jaxb:class>
<inheritance:implements>com.whatever.messages.Request</inheritance:implements>
</jaxb:bindings>
I receive the error message
Error while parsing schema(s).Location [ file:/C:/Users/justme/Documents/NetBeansProjects/someproject/src/main/binding/messages-in-binding.xjb{19,42}].
com.sun.istack.SAXParseException2; systemId: file:/C:/Users/justme/Documents/NetBeansProjects/someproject/src/main/binding/messsages-in-binding.xjb; lineNumber: 19; columnNumber: 42; compiler was unable to honor this class customization. It is attached to a wrong place, or its inconsistent with other bindings.
which reports that the location was
Error while generating code.Location [ file:/C:/Users/justme/Documents/NetBeansProjects/someproject/src/main/schema/MESSAGES-IN.xsd{106693,54}].
which happens to correspond to
<xs:complexType name="someLogin" mixed="true">
...
</xs:complexType>
Now, I've tried a second directive to bind the interface to the XSD element
<xs:element name="someLogin" type="someLogin" substitutionGroup="externalMethod"/>
But I just get the same error message with the element's line number as the location.
Obviously one wants to attach an interface to a class, and all of the examples look pretty close to my bindings file, but something must be wrong.
My environment is
Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600)
Maven home: C:\Program Files\NetBeans 7.2.1\java\maven
Java version: 1.7.0_07, vendor: Oracle Corporation
Java home: C:\Program Files (x86)\Java\jdk1.7.0_07\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
Can someone explain why xjc believes the extension is operating on the wrong XSD type?
In the construction
<jaxb:bindings
node="//Xs:complexType[#name='someLogin">
...
</jaxb:bindings>
the node attribute is expected to be an XPath expression. (Or so it says at Oracle). Try adding the closing square bracket to make it one, and see if that helps.

Create a common xsd generated class to be used by other packages

I am trying to use the same generated class but in separate packages. So the structure should look something like this:
com.test.common
-commonType.java
com.test.A
-objectA.java
com.test.B
-objectB.java
But i keep getting this:
com.test.common
-commonType.java
com.test.A
-objectA.java
-commonType.java
com.test.B
-objectB.java
-commonType.java
My common.xsd looks like this:
<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" version="1.0"
targetNamespace="http://test.com/magic/common"
xmlns="http://test.com/magic/common"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<xs:complexType name="CommonType">
<xs:sequence>
<xs:element name="name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
the objectA.xsd looks like
<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" version="1.0"
targetNamespace="http://test.com/magic/objectA"
xmlns:common="http://test.com/magic/common"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<xs:complexType name="ObjectA">
<xs:sequence>
<xs:element name="size" type="xs:string" />
<xs:element name="commonA" type="common:CommonType" />
</xs:sequence>
</xs:complexType>
</xs:schema>
And objectB.xsd looks like:
<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" version="1.0"
targetNamespace="http://test.com/magic/objectB"
xmlns:common="http://test.com/magic/common"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<xs:complexType name="ObjectB">
<xs:sequence>
<xs:element name="version" type="xs:string" />
<xs:element name="commonB" type="common:CommonType" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I have a common binding file common.xjb which looks like this:
<jxb:bindings schemaLocation="../xsd/common.xsd" node="/xsd:schema">
<jxb:schemaBindings>
<jxb:package name="com.test.common"/>
</jxb:schemaBindings>
</jxb:bindings>
And finally my maven job looks like this:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<args>
<arg>-Xequals</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.3</version>
</plugin>
</plugins>
<episode>true</episode>
<extension>true</extension>
<verbose>true</verbose>
<generateDirectory>src/main/java</generateDirectory>
</configuration>
<executions>
<execution>
<id>common</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.test.common</generatePackage>
<schemaIncludes>
<includeSchema>xsd/common.xsd</includeSchema>
</schemaIncludes>
</configuration>
</execution>
<execution>
<id>login</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.test.A</generatePackage>
<bindingIncludes>
<includeBinding>xjb/commons.xjb</includeBinding>
</bindingIncludes>
<schemaIncludes>
<includeSchema>xsd/objectA.xsd</includeSchema>
</schemaIncludes>
</configuration>
</execution>
<execution>
<id>alert</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.test.B</generatePackage>
<bindingIncludes>
<includeBinding>xjb/commons.xjb</includeBinding>
</bindingIncludes>
<schemaIncludes>
<includeSchema>xsd/objectB.xsd</includeSchema>
</schemaIncludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
There is a part in the maven-jaxb2-plugin documentation dedicated specifically to the separate schema compilation. But you can also achieve your goal with usual XJC bindings.
Do not use generatePackage in the plugin config, use jaxb:package in bindings, ex.:
<jaxb:schemaBindings>
<jaxb:package name="generatePackage"/>
</jaxb:schemaBindings>
Use <jaxb:class ref="com.test.common.CommonType"/> on commonType in xjb/commons.xjb.
SO Disclaimer: I'm the author of the maven-jaxb2-plugin.
You can use the -episode extension in the JAXB XJC to handle this use case:
XJC call for common.xsd
xjc -d out -episode common.episode common.xsd
XJC call for objectA.xsd
The episode file produced from the first step is really a JAXB external bindings file that contains links between schema types and existing classes. This prevents XJC from regenerating these classes.
xjc -d out objectA.xsd -extension -b common.episode
For a Detailed Example
Can Castor handle class generation from multiple XSDs importing from a base XSD?

JAXB External Custom Binding XJC Issue - Parsing results in empty node

Forgive me if this is a duplicate. Here is my binding.xjb file. But now i am getting the regular error that the complex type target "AddBankVaultRplyType" is not found. I don't see any issue. Can somebody help me with this? I am listing the xsd that i am trying to customize
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:pd="http://chubb.com/cpi/polsvc/xmlobj"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jxb:extensionBindingPrefixes="inheritance"
jxb:version="2.1"
>
<jxb:bindings node="/xs:schema/xs:ServiceReply/xs:complexType[#name='AddBankVaultRplyType']">
<inheritance:extends>com.print.poc.AddressTypeHelper</inheritance:extends>
</jxb:bindings>
Here is the piece of XSD that i am trying to customize
<xs:schema xmlns:pd="http://com/polsvc/xmlobj" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://com/polsvc/xmlobj" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="AddBankVaultRplyType">
</xs:complexType>
<xs:element name="ServiceReply">
<xs:complexType>
<xs:sequence>
<xs:element name="ReplyHeader" type="pd:MsgHeaderType"/>
<xs:element name="RequestHeader" type="pd:MsgHeaderType"/>
<xs:choice>
<xs:element name="AddBankVaultReply" type="pd:AddBankVaultRplyType"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Now if i run XJC it is saying me that the target "/xs:schema/xs:ServiceReply/xs:complexType[#name='AddBankVaultRplyType']" results in empty node. What is the mistake i am doing here
You will need to wrap in a bindings that has the schema location set. It should be something like:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:pd="http://chubb.com/cpi/polsvc/xmlobj"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jxb:extensionBindingPrefixes="inheritance"
version="2.1">
<jxb:bindings schemaLocation="your-schema.xsd">
<jxb:bindings node="//xs:complexType[#name='AddBankVaultRplyType']">
<inheritance:extends>com.print.poc.AddressTypeHelper</inheritance:extends>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
For more information:
http://jaxb.java.net/guide/Dealing_with_errors.html
I finally got mine workign with subclassing as well as adding #XmlRootElement to those dang complexTypes that are used by a root element(I don't get why JAXB doesn't add it for me, but this does the trick of doing that since JAXB doesn't)
<?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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:annox="http://annox.dev.java.net"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd
http://annox.dev.java.net "
jaxb:extensionBindingPrefixes="xjc annox"
version="2.1">
<jaxb:globalBindings>
<jaxb:serializable uid="1"/>
<!-- All generated classes must have MySignature interface (supplied in dependencies) -->
<xjc:superClass name="com.cigna.framework.DataObject"/>
<xjc:superInterface name="com.cigna.framework.InterfaceTest"/>
<!-- All temporal fields are implemented as Joda DateTime and use DateUtils as an adapter -->
<jaxb:javaType
name="org.joda.time.DateTime"
xmlType="xs:time"
parseMethod="com.cigna.framework.util.DateUtil.stringToDateTime"
printMethod="com.cigna.framework.util.DateUtil.dateTimeToString"
/>
</jaxb:globalBindings>
<!-- Application of annotations to selected classes within schemas -->
<!-- org.example.SomeRootType #XmlRootElement -->
<jaxb:bindings schemaLocation="../schemas/externalaction_2012_03.xsd" node="/xs:schema">
<jaxb:schemaBindings >
<jaxb:package name="com.framework.action"></jaxb:package>
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="../schemas/common_2012_04.xsd" node="/xs:schema">
<jaxb:schemaBindings >
<jaxb:package name="com.framework.common"></jaxb:package>
</jaxb:schemaBindings>
<jaxb:bindings node="xs:complexType[#name='PersonNameType']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="SomeRootType"/>
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="../schemas/utilities_2012_03.xsd" node="/xs:schema">
<jaxb:schemaBindings >
<jaxb:package name="com.framework.util"></jaxb:package>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
Of course I struggled with the pom.xml alot but finally came to this solution which worked for me.
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<id>process-xsd</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<schemaIncludes>
<include>schemas/*.xsd</include>
</schemaIncludes>
<bindingIncludes>
<include>schemas/*.xjb.xml</include>
</bindingIncludes>
<generateDirectory>${project.build.directory}/generated-sources</generateDirectory>
<extension>true</extension>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.3</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.3</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>
later,
Dean

Compiling multiple schemas into different packages using JAXB 2.1

I have a CommonTypes.xsd which I'm including in my all other XSDs using xs:include. I get
Multiple <schemaBindings> are defined for the target namespace ""
when I try to compile it into different packages using binding files. Please tell me whether there is a way to compile them into different packages. I'm using jaxb 2.1
Yeah, there is a way.
Assuming:
xsd/common/common.xsd
xsd/foo/foo.xsd
In the common directory place common.xjb:
<jxb:schemaBindings>
<jxb:package name="mypkg.common">
</jxb:package>
</jxb:schemaBindings>
In the foo directory place foo.xjb:
<jxb:schemaBindings>
<jxb:package name="mypkg.foo">
</jxb:package>
</jxb:schemaBindings>
In the build.xml file, create one xjc task for each:
<xjc destdir="${app.src}" readOnly="true" package="mypkg.common">
<schema dir="${app.schema}/common" includes="*.xsd" />
<binding dir="${app.schema}/common" includes="*.xjb" />
</xjc>
<xjc destdir="${app.src}" readOnly="true" package="mypkg.foo">
<schema dir="${app.schema}/foo" includes="*.xsd" />
<binding dir="${app.schema}/foo" includes="foo.xjb" />
</xjc>
You need to make sure that common.xsd has a targetNameSpace that is different from foo.xsd's targetNameSpace.
As stated already by Ben there is no way to do that if they have the same namespace.
But how to do it if you do have different namespaces?
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<jxb:bindings namespace="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" schemaLocation="oagi/Common/UNCEFACT/ATG/CoreComponents/UnqualifiedDataTypes.xsd" >
<jxb:schemaBindings>
<jxb:package name="com.test.oagi.udt"/>
</jxb:schemaBindings>
</jxb:bindings>
<jxb:bindings namespace="http://www.openapplications.org/oagis/9/codelists" schemaLocation="oagi/Common/OAGi/Components/CodeLists.xsd" >
<jxb:schemaBindings>
<jxb:package name="com.test.oagi.cl"/>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
but be sure you do not use the command line parameter -p, since that will override your config.
I've meet the same problem and haven't solve it yet, but I'm afraid that it can't be possible to generate XSD into differents packages :
It is not legal to have more than one <jaxb:schemaBindings> per namespace, so it is impossible to have two schemas in the same target namespace compiled into different Java packages
from Compiler Restrictions at the end of this page
but if some one find some work around, just inform us please
I know it is an old post, but, as there is no answer for the exact question, here is my proposal:
As mmoossen explained, the trick is to specify different namespaces for the XSDs.
But, adding a namespace attribute in the jxb:bindings tag doesn't work:
<jxb:bindings namespace="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" schemaLocation="oagi/Common/UNCEFACT/ATG/CoreComponents/UnqualifiedDataTypes.xsd" >
Instead of that, you need to add a targetNamespace attribute to the xs:schema tags of your XSDs:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified"
targetNamespace="some.namespace"
version="1.0">
Once done, you will be able to have 1 external customization file (.xjb) declaring different schemaBindings, each of them possibly using a different package:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
jaxb:extensionBindingPrefixes="xjc annox inherit">
<jaxb:bindings schemaLocation="MyFirstXSD.xsd">
<jaxb:schemaBindings>
<jaxb:package name="com.test.a" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="MySecondXSD.xsd">
<jaxb:schemaBindings>
<jaxb:package name="com.test.b" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="MyThirdXSD.xsd">
<jaxb:schemaBindings>
<jaxb:package name="com.test.c" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
Can be done as mentioned in jaxb maven plugin usage page in case of having Multiple schemas with different configuration.
Separate packages can be configured for each schema.
<packageName>se.west</packageName>
complete example configuration below:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>xjc-schema1</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<!-- Use all XSDs under the west directory for sources here. -->
<sources>
<source>src/main/xsds/west</source>
</sources>
<!-- Package name of the generated sources. -->
<packageName>se.west</packageName>
</configuration>
</execution>
<execution>
<id>xjc-schema2</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<!-- Use all XSDs under the east directory for sources here. -->
<sources>
<source>src/main/xsds/east</source>
</sources>
<!-- Package name of the generated sources. -->
<packageName>se.east</packageName>
<!--
Don't clear the output directory before generating the sources.
Clearing the output directory removes the se.west schema from above.
-->
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>

Resources