Single file for multiple WSDL binding - java

I am currently developing SOAP webservices client using Apache CXF's cxf-codegen-plugin. Since I have multiple WSDL, I need to bind it to different packages in my java project.
My question is, is it possible to define 1 single binding file for multiple WSDL file?
Below is my plugin configuration
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<defaultOptions>
<bindingFiles>
<bindingFile>src/main/resources/wsdl/bindings.xjb</bindingFile>
</bindingFiles>
</defaultOptions>
<sourceRoot>${basedir}/src/main/java</sourceRoot>
<wsdlRoot>src/main/resources/wsdl</wsdlRoot>
<includes>
<include>*.wsdl</include>
</includes>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
I am trying to achieve something like this but to no avail
<jaxws:bindings
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns="http://java.sun.com/xml/ns/jaxws">
<jaxws:bindings wsdlLocation="serviceA.wsdl" >
<jaxws:package name="org.ws.serviceA"/>
</jaxws:bindings>
<jaxws:bindings wsdlLocation="serviceB.wsdl" >
<jaxws:package name="org.ws.serviceB"/>
</jaxws:bindings>
</jaxws:bindings>

Turns out, it is indeed impossible and clearly stated in this site
https://jax-ws.java.net/nonav/2.1.2/docs/customizations.html
1.1.1 Root Binding Element
The jaxws:bindings declaration appears as the root of all other
binding declarations. This top-level jaxws:bindings element must
specify the location of the WSDL file as a URI in the value of
wsdlLocation attribute.
However it did not specify anything about the wsdlLocation at child element. This site does http://itdoc.hitachi.co.jp/manuals/3020/30203Y2310e/EY230286.HTM#ID00669
Non-root jaxws:bindings > wsdlLocation > The attribute cannot be
specified. Even if the attribute is specified, it is ignored.
Hopefully this can be improved in the future as JAXB can already bind multiple schemaLocation in one file like this
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="schema1.xsd" node="//xsd:schema">
<jxb:schemaBindings>
<jxb:package name="org.package1" />
</jxb:schemaBindings>
</jxb:bindings>
<jxb:bindings schemaLocation="schema2.xsd" node="//xsd:schema">
<jxb:schemaBindings>
<jxb:package name="org.package2" />
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>

Related

JAXB XJC: how to resolve XSD to Java property conflicts?

I'm trying to generate Java classes starting from an XML Schema Definition
but I'm getting an error about a Property "Lang" is already defined.
[ERROR] http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd [302,52]
com.sun.istack.SAXParseException2: Property "Lang" is already defined. Use <jaxb:property> to resolve this conflict.
[ERROR] http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd [303,35]
com.sun.istack.SAXParseException2
The XSD I'm using defines the Common Weakness Enumeration (CWE) and is located at https://cwe.mitre.org/data/xsd/cwe_schema_v6.10.xsd
A short command to reproduce the error is:
xjc http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd
This is my pom.xml
<build>
<plugins>
<!-- https://www.mojohaus.org/jaxb2-maven-plugin/#/repo -->
<!-- https://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v3.1.0/index.html -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<sources>
<source>_schema_/cwe</source>
</sources>
<xjbSources>
<xjbSource>${basedir}/cti-domain/src/main/xjb</xjbSource>
</xjbSources>
<outputDirectory>${basedir}/cti-domain/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
and this is my attempt to fix the error:
<!-- cti-domain/src/main/xjb/cwe-bindings.xjb -->
<jxb:bindings version="1.0"
xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="../../../../_schema_/cwe/cwe_schema_v6.10.xsd" node="//xsd:schema">
<jxb:schemaBindings>
<jxb:package name="com.example.cwe"/>
</jxb:schemaBindings>
<!-- <jxb:bindings schemaLocation="http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd" node="//xsd:schema">-->
<!-- <jxb:property name="Language"/>-->
<!-- </jxb:bindings>-->
</jxb:bindings>
</jxb:bindings>
You need to rename the 'lang' property at line 302 & line 1166 in http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd to something that does not clash, eg 'langAttribute'
The first 'lang' is in the last line in the snippet of xhtml1-strict.xsd below:
<xs:element name="bdo">
<xs:annotation>
<xs:documentation>
I18N BiDi over-ride
</xs:documentation>
</xs:annotation>
<xs:complexType mixed="true">
<xs:complexContent>
<xs:extension base="Inline">
<xs:attributeGroup ref="coreattrs"/>
<xs:attributeGroup ref="events"/>
<xs:attribute name="lang" type="LanguageCode"/>
This is what should be in the xjb file:
<!-- cti-domain/src/main/xjb/cwe-bindings.xjb -->
<jxb:bindings version="3.0"
xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd" node="//xsd:schema">
<jxb:schemaBindings>
<jxb:package name="com.example.cwe"/>
</jxb:schemaBindings>
<!-- rename the 'lang' attribute on line 302 to 'langAttribute' -->
<jxb:bindings node="//xsd:attributeGroup[#name='i18n']/xsd:attribute[#name='lang']">
<jxb:property name="langAttribute"/>
</jxb:bindings>
<!-- rename the 'lang' attribute on line 1166 to 'langAttribute' -->
<jxb:bindings node="//xsd:element[#name='bdo']/xsd:complexType/xsd:complexContent/xsd:extension/xsd:attribute[#name='lang']">
<jxb:property name="langAttribute"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
I have tested with jaxb-ri-4.0.1 w jdk-17.0.2_8* with command line:
xjc http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd -b cwe-bindings.xjb
Output starts like this:
parsing a schema...
compiling a schema...
com\example\cwe\A.java
com\example\cwe\AContent.java
com\example\cwe\Abbr.java
also works with jaxb-ri-2.3.0 and jdk1.8.0_231 - with jxb:bindings version="1.0" and xmlns:jxb="http://java.sun.com/xml/ns/jaxb"

Duplicate NS in SOAP Message

Ive got a problem with my xml SOAP message - using java 1.8, Websphere 8.5.5.17, for generating class from wsdl Ive used jaxws-maven-plugin.
When I received SOAP message via WAS to my Java application, something duplicate my ns3 prefix (sometimes another), and unmarshaling to type Object is not work :(
<ns3:document xmlns:ns4="http://service.gggg.com/common/CommonMessage/v01" xmlns:ns3="http://service.gggg.com/RDS/datamodel/LAA/common/v01">
<ns3:outputChannels>
<ns3:archiveType>None</ns3:archiveType>
<ns3:publishingType>Signature</ns3:publishingType>
<ns3:printType>None</ns3:printType></ns3:document>
ns3 prefix is correctly defined upper and element "document" is anyType. The problem is in the inner atribute of document element -> the second definiton of xmlns:ns3, when I overide it manualy together with ns of childs it works.
<ns3:document xmlns:ns4="http://service.gggg.com/common/CommonMessage/v01" xmlns:ns9="http://service.gggg.com/RDS/datamodel/LAA/common/v01">
<ns9:outputChannels>
<ns9:archiveType>None</ns9:archiveType>
<ns9:publishingType>Signature</ns9:publishingType>
<ns9:printType>None</ns9:printType></ns3:document>
Ive just tried jaxb NamespacePrefixMapper, namespaces set in jaxb bindings, manualy overiding in Interceptor SOAPElement, add prefix namespace in package-info.java and a lot lot lot of more but without happy end :(
maven
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<genJWS>false</genJWS>
<extension>true</extension>
<wsdlFiles>
<wsdlFile>
${project.basedir}/src/main/resources/wsdl/ServiceWSDL.wsdl
</wsdlFile>
</wsdlFiles>
<target>2.0</target>
<xadditionalHeaders>true</xadditionalHeaders>
<bindingFiles>
<bindingFile>${project.basedir}/src/main/resources/jaxb/bindings.xml</bindingFile>
<bindingFile>${project.basedir}/src/jaxws/binding.xml</bindingFile>
</bindingFiles>
</configuration>
</execution>
</executions>
</plugin>
binding.xml
<jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
<jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle></jaxws:bindings>
bindings.xml
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<jaxb:bindings>
<jaxb:globalBindings generateElementProperty="false" fixedAttributeAsConstantProperty="true"
choiceContentProperty="true"/>
</jaxb:bindings></jaxb:bindings>
Can someone help me ?
THX!

JAXWS: How to change CXF-generated classes names defined in external XSD?

I'm trying to change name of a class generated from wsdl (I don't want to modify any wsdl or xsd file directly). The problem is that its definition is in a separate xsd file.
The structrure of wsdl looks like this:
main.wsdl:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://foo.bar/zee">
<wsdl:import location="typedef.wsdl" namespace="http://foo.bar/wee">
</wsdl:import>
...
</wsdl:definitions>
typedef.wsdl:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Foo" targetNamespace="http://foo.bar/wee">
<wsdl:types>
<xsd:schema>
<xsd:import namespace="http://foo.bar/wee/schema" schemaLocation="FooBar.xsd"/>
</xsd:schema>
</wsdl:types>
...
<wsdl:definitions>
FooBar.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://foo.bar/wee/schema">
...
<xsd:complexType name="FooType">
<xsd:sequence>
...
</xsd:complexType>
</xsd:schema>
Now let's say I want to rename the FooType class to Foo. After reading this: JAXB: How to change XJC-generated classes names when attr type is specified in XSD? I've created a following bindings file.
jaxws_bindings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1"
wsdlLocation="http://127.0.0.1:8431/Foo/types.wsdl">
<jxb:bindings schemaLocation="http://127.0.0.1:8431/Foo/FooBar.xsd">
<jxb:bindings node="//xs:complexType[#name='FooType']">
<jxb:class name="Foo"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
But all I get is an error:
[ERROR] Failed to execute goal org.apache.cxf:cxf-codegen-plugin:2.7.0:wsdl2java (generate-sources) on
project foo: Execution generate-sources of goal org.apache.cxf:cxf-codegen-plugin:2.7.0:wsdl2java failed:
file:/E:/sources/.../jaxws_bindings.xml [8,95]: "http://127.0.0.1:8431/Foo/FooBar.xsd" is not a part of
this compilation. Is this a mistake for "http://127.0.0.1:8431/Foo/FooBar.xsd"? -> [Help 1]
I've already tried everything that came to my mind, but still got nothing close to a success.
Anyone happens to know how to do this?
PS: To generate the classes I use maven cxf codegen plugin with a following configuration in pom.xml:
<build>
<finalName>${project.groupId}.${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.7.0</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated-sources</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>http://127.0.0.1:8431/Foo/main.wsdl</wsdl>
<extraargs>
<extraarg>-client</extraarg>
</extraargs>
<bindingFiles>
<bindingFile>jaxws_bindings.xml</bindingFile>
</bindingFiles>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I figured this out based on this gist.
While this works with XJC:
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="1.0">
<jaxb:bindings schemaLocation="..."
node="/xsd:schema/xsd:element[#name='Bar']">
<jaxb:class name="Foo"/>
</jaxb:bindings>
</jaxb:bindings>
you need this with CXF:
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="1.0">
<jaxb:bindings schemaLocation="..."
node="/xsd:schema/xsd:complexType[#name='Case']">
<jaxb:class name="Wat" implClass="bar"/>
</jaxb:bindings>
</jaxb:bindings>
The difference is element vs complexType.

Customizing Java packages JAXB wsimport

I'm trying to generate a client with maven and jaxb from a wsdl file with 2 schemas inside and some elements with the same name from different schemas
When I try to execute the compilation I'm getting the next error:
Two declarations cause a collision in the ObjectFactory class.
WSDL schemas:
<wsdl:types>
<schema targetNamespace="http://ws.services" xmlns="http://www.w3.org/2001/XMLSchema">...</schema>
<schema targetNamespace="http://ws.models" xmlns="http://www.w3.org/2001/XMLSchema">...</schema>
</wsdl:types>
I tried renaming the elements that produce the error, but then my spring client receive the correct SOAP message, but it doesn't populate the response object properly (all its attributes are null). I guess the problem might come from renaming the response classes, so that's why I'm trying to generate different packages keeping the original name of all the classes.
In order to do that, I wrote the next bindings file but I don't know what I'm doing wrong that it is not working.
bindings.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<jaxb:bindings schemaLocation="mywsdl.wsdl#types?schema1"
node="/xs:schema[#targetNamespace='http://ws.services']">
<jaxb:schemaBindings>
<jaxb:package name="package1" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="mywsdl.wsdl#types?schema2"
node="/xs:schema[#targetNamespace='http://ws.models']">
<jaxb:schemaBindings>
<jaxb:package name="package2" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
My configuration part in the maven file is the next, just in case it is useful:
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlLocation>wsdl/mywsdl.wsdl</wsdlLocation>
<wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>mywsdl.wsdl</wsdlFile>
</wsdlFiles>
<bindingDirectory>src/main/resources/wsdl</bindingDirectory>
<bindingFiles>
<bindingFile>bindings.xml</bindingFile>
</bindingFiles>
<packageName>original.package</packageName>
<sourceDestDir>${basedir}/src/main/java</sourceDestDir>
</configuration>
When I compile with this bindings files, the same error appears. So I think that maybe it isn't right.
Do you find any mistakes?
Thanks.
From my experience it is best to create 2 binding files (one for each WSDL file).
Update your pom.xml accordingly and make sure that the root element of the binding files is jaxws:bindings (and not jaxb:bindings!)
Some hints:
Make sure to set the "wsdlLocation" attribute correctly! It must point to the WSDL file using a relative path!
The jaxws:package determines the package that will be used for the generated service classes. (the stuff annotated with #WebService)
Enable or disable wrapperStyle and asyncMapping as you wish. ;-)
Example binding file for "package1":
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
wsdlLocation="mywsdl.wsdl"
version="2.0">
<jaxws:package name="package1"/>
<jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
<jaxws:enableAsyncMapping>true</jaxws:enableAsyncMapping>
<jaxws:bindings node="//wsdl:definitions/wsdl:types/xs:schema[#targetNamespace='http://ws.services']">
<jaxb:schemaBindings>
<jaxb:package name="package1"/>
</jaxb:schemaBindings>
</jaxws:bindings>
</jaxws:bindings>

Is there a way to deal with duplicate element definitions across multiple .xsd files in JAXB?

I have dozens and dozens .xsd files that I want to auto-generate code for. A couple of the files have duplicate names that clash when I try to generate all of them at the same time.
I am focusing on just trying to get 2 of these to work.
When I get these 2 working I will fix the rest. But I am just focusing on 2 of these files for now. I am not in control of them, they are from a vendor and follow a "standard", so editing them is not an option for multiple reasons.
I am using the maven-jaxb2-plugin to process these files.
I added a binding.xjb file as suggested in the link in mat b's answer and other instructions I have found on the web. But I am getting the following errors, an no output.
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings version="2.1"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation=" http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">
<jxb:bindings schemaLocation="mac-3.4.xsd">
<jxb:schemaBindings>
<jxb:package name="my.company.mac"/>
</jxb:schemaBindings>
</jxb:bindings>
<jxb:bindings schemaLocation="mac-stylesheet-3.4.xsd">
<jxb:schemaBindings>
<jxb:package name="my.company.stylesheet"/>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
gives the following error
[ERROR] Error while parsing schema(s).Location [ file:/C:/Users/Jarrod%20Roberson/Projects/spa-tools/spa-lib/src/main/sc
hema/mac-stylesheet-3.4.xsd{165,33}].
org.xml.sax.SAXParseException: 'halign' is already defined
The offending element is : ( there are many others this is just the first one that clashes )
<xsd:simpleType name="halign">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="left" />
<xsd:enumeration value="center" />
<xsd:enumeration value="right" />
</xsd:restriction>
</xsd:simpleType>
And is identical in each of the .xsd files, how do I resolve this duplication with either only one class being generated or each one being generated in to their own package namespace?
This isn't the only duplicate element like this there are lots of them, so just trying to remove them from the files isn't a option either.
This halign is in multiple .xsd files and I want to either put them in their individual packages, or be able to tell the compiler to use the first one that was generated.
This is where I started off before I tried an external .xjb file, by just specifying the package in the pom.xml.
How do I configure the binding to either ignore the duplicate configurations, map them to separate packages or map them to existing implementations?
I have a half-way workaround solution to this, but it is very time intensive.
I had to create a separate <execution/> for every one of the files that has duplicate entries.
<executions>
<execution>
<id>jaxb-mac</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<forceRegenerate>true</forceRegenerate>
<generatePackage>my.company.mac</generatePackage>
<schemaDirectory>src/main/schema</schemaDirectory>
<schemaIncludes>
<include>mac-3.4.xsd</include>
</schemaIncludes>
</configuration>
</execution>
<execution>
<id>jaxb-stylesheet</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<forceRegenerate>true</forceRegenerate>
<generatePackage>my.company.stylesheet</generatePackage>
<schemaDirectory>src/main/schema</schemaDirectory>
<schemaIncludes>
<include>mac-stylesheet-3.4.xsd</include>
</schemaIncludes>
</configuration>
</execution>
The <forceRegenerate>true</forceRegenerate> is important or only the first <execution/> will run and the rest will think that there is no need to run because I am generating into the same directory.
I would still like a less labor intensive solution to deal with the duplicates.
I think if I make the first master .xsd a separate module that builds into its own .jar file, I could then use the <episode/> tag and have it skip generating the same duplicate elements over and over, since they are identical in definition.
I have since decided to abandon XML if at all possible and JAXB completely. There are newer and better ways to parse XML and map it to objects as of this edit.
Realize this is old, but I had the same error and could it resolve it by not specifying a target package, i.e. the -b option with xjc. Then the duplicate elements each get created in their own namespace package and no conflict.
I post my solution for gradle, it solves duplicate issue and does`not require xjb files:
task generateJaxb() {
ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
ext.classesDir = "${buildDir}/classes/jaxb"
ext.schemaDir = "src/resources/schemas"
ext.tmp = "${buildDir}/tmp/xjc"
doLast() {
project.ant {
taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
classpath: configurations.jaxb.asPath
delete(dir: tmp)
mkdir(dir: tmp)
delete(dir: sourcesDir)
delete(dir: classesDir)
mkdir(dir: sourcesDir)
mkdir(dir: classesDir)
}
fileTree(schemaDir){
include '**/*.xsd'
include '**/*.wsdl'
}.each{File file->
//println file
project.ant {
taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
classpath: configurations.jaxb.asPath
xjc(destdir: tmp, schema:"${file.getAbsolutePath()}") {
arg(value: "-wsdl")
produces(dir: tmp, includes: "**/*.java")
}
copy(todir: sourcesDir) {
fileset(dir: tmp, erroronmissingdir: false) {
include(name: "**/*.java")
}
}
delete(dir: tmp)
mkdir(dir: tmp)
}
}
project.ant {
javac(destdir: classesDir, source: 1.6, target: 1.6, debug: true,
debugLevel: "lines,vars,source",
classpath: configurations.jaxb.asPath) {
src(path: sourcesDir)
include(name: "**/*.java")
include(name: "*.java")
}
copy(todir: classesDir) {
fileset(dir: sourcesDir, erroronmissingdir: false) {
exclude(name: "**/*.java")
}
}
}
}
}
configurations {
jaxb
}
dependencies {
jaxb("com.sun.xml.bind:jaxb-xjc:2.2.4-1")
compile(files(generateJaxb.classesDir).builtBy(generateJaxb))
}
jar {
from generateJaxb.classesDir
}
Add a <schemaBindings> element to the XSD:
<schemaBindings>
<package name="com.whatever.stuff" />
</schemaBindings>
Source
We had a similar problem: we had one wsdl file and two xsd files in the same directory. The wsdl file imports the two xsd files. The problem was that JAXB was taking all three files into consideration and throwing '... is already defined' error. It was basically complaining that it saw the same element in both wsdl and xsd file.
We could fix this issue in the maven plugin configuration (in pom.xml) by adding an exclude tag as in the following example:
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>mywsdl.wsdl</generatePackage>
<args><arg>-XautoNameResolution</arg></args>
<schemas>
<schema>
<fileset>
<excludes>
<exclude>*.xsd</exclude>
</excludes>
</fileset>
</schema>
</schemas>
</configuration>
</plugin>
</plugins>
</build>

Categories