I am using JAXB to generate classes from my XSD file. I would like to have the classes that are generated implement a common interface. So I am trying out the JAXB2 Basics plugin with the external binding file approach to do this. This is my custom binding file:
customBindingFile.xjb
<?xml version="1.0"?>
<jxb:bindings version="1.0"
xmlns:jxb="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:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jxb:extensionBindingPrefixes="xjc">
<jxb:bindings schemaLocation="abc-api.xsd">
<jxb:bindings node="//xs:complexType[@name='MyClass']">
<inheritance:implements>com.kuldeep.CommonInterface</inheritance:implements>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
Following is my maven plugin in pom file for source generation: Note the comment added by me is the change I made to this existing plugin entry.
pom.xml
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.plugin.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<!-- **extensions and args added by me** -->
<extensions>
<extension>org.jvnet.jaxb2_commons:jaxb2-basics:0.9.2</extension>
</extensions>
<args>
<arg>-Xinheritance</arg>
</args>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<defaultOptions>
<bindingFiles>
<bindingFile>src/main/resources/jaxws_binding.xml</bindingFile>
<bindingFile>src/main/resources/jaxb_binding.xml</bindingFile>
</bindingFiles>
</defaultOptions>
<wsdlOptions>
......
<wsdlOption>
<wsdl>${project.build.directory}/generated/framework/cxf/abc-api-inline.wsdl</wsdl>
<!-- **bindingFile added by me** -->
<bindingFile>src/main/resources/customBindingFile.xjb</bindingFile>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<!-- **dependency added by me** -->
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.9.2</version>
</dependency>
</dependencies>
</plugin>
The issue that I have is my schema file abc-api.xsd resides in some other project, so when I try to do maven install to generate my classes, I get error saying abc-api.xsd is not a part of this compilation.
[ERROR] Failed to execute goal org.apache.cxf:cxf-codegen-plugin:3.0.3:wsdl2java (generate-sources) on project : Execution generate-sources of goal org.apache.cxf:cxf-codegen-plugin:3.0.3:wsdl2java failed: file:/I:/project/src/main/resources/customBindingFile.xjb [9,56]: "file:/I:/project/src/main/resources/abc-api.xsd" is not a part of this compilation. Is this a mistake for "file:/I:/project/src/main/resources/jaxb_binding.xml"? -> [Help 1]
And if I remove the schemaLocation attribute from customBindingFile.xjb it does not work and gives error:
XPath evaluation of "//xs:complexType[@name='MyClass']" results in empty target node
So my question is how can I avoid providing the specific schema file name/location in customBindingFile.xjb and just have it applied to whatever xsd it's using to generate the classes.
With help from our Architect, I was able to resolve this issue. I added a jaxws binding file and used prefix-less xpath query in there to match my request elements. This way I don't need to provide schema location anywhere and it will be applied to the specific WSDL based on the XPath query.
jaxws_binding_inheritance.xml
<jaxws:bindings version="2.0" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
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="inheritance xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1">
<jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
<jaxws:bindings
node="*[local-name()='definitions']/*[local-name()='types']/*[local-name()='schema' and
(@targetNamespace='urn:net:mycompany:api:abc')]">
<jaxb:bindings
node="//*[local-name()='element' and
not(@name = 'ExcludeThisRequest' or @name = 'AlsoExcludeThisRequest') and
(substring(@name, string-length(@name) - string-length('Request') +1) = 'Request')]/*[local-name()='complexType']">
<inheritance:implements>com.kuldeep.CommonRequest</inheritance:implements>
</jaxb:bindings>
</jaxws:bindings>
</jaxws:bindings>
And added that jaxws binding file (jaxws_binding_inheritance.xml) under wsdloption
for the wsdl where I wanted to apply that.
pom.xml
<wsdlOption>
<wsdl>${project.build.directory}/generated/framework/cxf/abc-api-inline.wsdl</wsdl>
<bindingFiles>
<bindingFile>src/main/resources/jaxws_binding_inheritance.xml</bindingFile>
</bindingFiles>
</wsdlOption>