使用jaxws-maven-plugin生成代码时遇文件名冲突问题求助
解决JAX-WS生成代码时的大小写命名冲突问题
问题背景
你的WSDL中同时存在全大写的PROVIDER操作/属性,以及首字母大写的Provider复杂类型。由于Windows文件系统不区分文件名大小写,使用jaxws-maven-plugin生成代码时,会出现PROVIDER.java和Provider.java的命名冲突,导致编译失败。
WSDL中的冲突元素
1. 同名的PROVIDER操作
<operation name="PROVIDER"> <input message="s0:PROVIDERSoapIn"/> <output message="s0:PROVIDERSoapOut"/> </operation> <operation name="PROVIDER"> <soap:operation soapAction="http://someSoapAtion" style="document"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation>
2. PROVIDER属性
<s:attribute name="PROVIDER" type="s:string"/>
3. Provider复杂类型
<s:complexType name="ArrayOfProviderProvider"> <s:sequence> <s:element maxOccurs="unbounded" minOccurs="0" name="Provider" nillable="true" type="s0:Provider"/> </s:sequence> </s:complexType> <s:complexType name="Provider"> <s:attribute name="SYSCODE" use="required"> <s:simpleType> <s:restriction base="s:string"> <s:maxLength value="25"/> </s:restriction> </s:simpleType> </s:attribute> .... </s:complexType>
当前使用的jaxws-maven-plugin配置
<plugin> <!--https://github.com/mojohaus/jaxws-maven-plugin/issues/54--> <groupId>com.helger.maven</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>2.6.2</version> <executions> <execution> <goals> <goal>wsimport</goal> </goals> </execution> </executions> <configuration> <vmArgs> <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg> </vmArgs> <args> <arg>-B-XautoNameResolution</arg> </args> <verbose>true</verbose> <wsdlFiles> <wsdlFile>${project.basedir}/src/main/resources/META-INF/wsdl/our.wsdl.xml</wsdlFile> </wsdlFiles> <packageName>com.openpayment.client.kamp</packageName> </configuration> </plugin>
生成代码时的错误信息
[INFO] jaxws:wsimport args: [-keep, -s, 'ourpathtotarget\target\generated-sources\wsimport', -d, 'ourpathtotarget\target\classes', -verbose, -encoding, UTF-8, -Xnocompile, -B-XautoNameResolution, -p, com.openpayment.client.kamp, "file:/ourpath/src/main/resources/META-INF/wsdl/our.wsdl.xml"] parsing WSDL... [ERROR] A class/interface with the same name "com.openpayment.client.kamp.PROVIDER" is already in use. Use a class customization to resolve this conflict. line 2639 of file:ourpathtosrc/src/main/resources/META-INF/wsdl/AWGW.WS.xml [ERROR] (Relevant to above error) another "Provider" is generated from here. line 2691 of file:ourpathtosrc/src/main/resources/META-INF/wsdl/AWGW.WS.xml [ERROR] This error is caused because on Windows you cannot have both "Provider.java" and "PROVIDER.java" in the same directory. Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at org.codehaus.mojo.jaxws.Invoker.main(Invoker.java:109) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.mojo.jaxws.Invoker.main(Invoker.java:99) Caused by: com.sun.tools.ws.wscompile.AbortException at com.sun.tools.ws.processor.modeler.wsdl.JAXBModelBuilder.bind(JAXBModelBuilder.java:144) at com.sun.tools.ws.processor.modeler.wsdl.WSDLModeler.buildJAXBModel(WSDLModeler.java:2298) at com.sun.tools.ws.processor.modeler.wsdl.WSDLModeler.internalBuildModel(WSDLModeler.java:198) at com.sun.tools.ws.processor.modeler.wsdl.WSDLModeler.buildModel(WSDLModeler.java:141) at com.sun.tools.ws.wscompile.WsimportTool.buildWsdlModel(WsimportTool.java:444) at com.sun.tools.ws.wscompile.WsimportTool.run(WsimportTool.java:205) at com.sun.tools.ws.wscompile.WsimportTool.run(WsimportTool.java:183) ... 5 more
解决方法
由于没有独立的XSD文件,使用JAX-WS绑定自定义文件重命名冲突元素是最优方案,具体步骤如下:
1. 创建绑定自定义文件
在项目src/main/resources/META-INF目录下创建jaxws-bindings.xml,内容如下:
<jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:s="http://www.w3.org/2001/XMLSchema" version="2.0"> <!-- 重命名全大写的PROVIDER操作生成的接口 --> <jaxws:bindings wsdlLocation="our.wsdl.xml" node="wsdl:definitions/wsdl:portType/wsdl:operation[@name='PROVIDER']"> <jaxws:class name="ProviderOperation"/> </jaxws:bindings> <!-- 重命名PROVIDER属性对应的Java字段 --> <jaxws:bindings wsdlLocation="our.wsdl.xml" node="wsdl:definitions/wsdl:types/s:schema/s:complexType/s:attribute[@name='PROVIDER']"> <jaxb:property name="providerCode"/> </jaxws:bindings> </jaxws:bindings>
注意:根据WSDL实际结构调整
node路径,确保精准定位冲突元素。优先处理操作冲突,再处理属性冲突。
2. 修改Maven插件配置,引入绑定文件
在jaxws-maven-plugin的configuration节点中添加绑定文件配置:
<plugin> <groupId>com.helger.maven</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>2.6.2</version> <executions> <execution> <goals> <goal>wsimport</goal> </goals> </execution> </executions> <configuration> <vmArgs> <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg> </vmArgs> <args> <arg>-B-XautoNameResolution</arg> </args> <verbose>true</verbose> <wsdlFiles> <wsdlFile>${project.basedir}/src/main/resources/META-INF/wsdl/our.wsdl.xml</wsdlFile> </wsdlFiles> <!-- 新增绑定文件配置 --> <bindingFiles> <bindingFile>${project.basedir}/src/main/resources/META-INF/jaxws-bindings.xml</bindingFile> </bindingFiles> <packageName>com.openpayment.client.kamp</packageName> </configuration> </plugin>
3. 验证效果
执行mvn clean compile,wsimport会按照绑定文件的规则重命名冲突类/字段,避免Windows文件系统的大小写冲突问题。
其他可选方案
- 修改WSDL:如果有权限修改WSDL,直接将全大写的
PROVIDER操作/属性改名(比如改为ProviderService),这是最直接的方法。 - 跨平台编译:在Linux/macOS环境下编译(这类系统区分文件名大小写),但仅能临时规避问题,后续在Windows环境仍可能出现冲突。
内容的提问来源于stack exchange,提问作者Eljah
相关产品推荐
相关产品推荐

