Java 使用 jaxws-maven-plugin 控制 JAX-WS wsdlLocation 属性值(绝对路径)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9802487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:48:23  来源:igfitidea点击:

Controlling JAX-WS wsdlLocation attribute value's (absolute path) with jaxws-maven-plugin

javaweb-servicesmavenwsdljax-ws

提问by Alex

I have a JAX-WS-driven web service whose WSDL we generate a web service client from in another Maven module (which I'll call ws-consumer).

我有一个 JAX-WS 驱动的 Web 服务,我们从另一个 Maven 模块(我将称之为ws-consumer)生成 Web 服务客户端的 WSDL 。

For better or worse, we copy the "published WSDLs" (the version of the WSDL & XSDs that the service held/generated at point of release) to our src/wsdlfolder of ws-consumerand then use jaxws-maven-pluginfrom org.jvnet to generate a client using jaxws:wsimportwith the following (truncated) configuration:

无论好坏,我们都将“已发布的 WSDL”(服务在发布时保留/生成的 WSDL 和 XSD 的版本)src/wsdl复制到我们的文件夹中ws-consumer,然后jaxws-maven-plugin从 org.jvnet 使用jaxws:wsimport以下内容生成客户端(截断)配置:

    <plugin>
        <groupId>org.jvnet.jax-ws-commons</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.1</version>
        <executions>
            <execution>
                <!--phase>generate-sources</phase -->
                <goals>
                    <goal>wsimport</goal>
                </goals>
                <configuration>
                    <wsdlDirectory>src/main/resources/META-INF/wsdl/</wsdlDirectory>
                    <wsdlFiles>
                        <wsdlFile>MyWS/MyWS.wsdl</wsdlFile>
                    </wsdlFiles>
                </configuration>
            </execution>
        </executions>
    </plugin>

Now, the generated client code has the following annotations applied at the class level:

现在,生成的客户端代码在类级别应用了以下注释:

@WebServiceClient(name = "MyWS", targetNamespace = "http://myws/blah", wsdlLocation = "**file:/C:/some/absolute/path/src/main/resources/META-INF/wsdl/MyWS/MyWS.wsdl"**)

emphasis mine

强调我的

As you can hopefully see, the wsdlLocationattribute value has a hard-coded absolute path that is going to be incorrect when the service is deployed.

正如您希望看到的,wsdlLocation属性值有一个硬编码的绝对路径,在部署服务时会出错。

Is there any way I can "control" this by setting it to just META-INF/wsdl/MyWS/MyWS.wsdlor some other value?

有什么办法可以通过将其设置为 justMETA-INF/wsdl/MyWS/MyWS.wsdl或其他值来“控制”它?

采纳答案by McDowell

It is possible with the Codehaus plugin:

Codehaus 插件可以实现:

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>jaxws-maven-plugin</artifactId>
   <version>1.9</version>
   <executions>
     <execution>
       <goals>
         <goal>wsimport</goal>
       </goals>
     </execution>
   </executions>
   <configuration>
     <keep>true</keep>
     <verbose>true</verbose>
     <wsdlDirectory>../wscontract/src/main/resources/wsdl</wsdlDirectory>
     <wsdlLocation>wsdl/MaintainAddress.wsdl</wsdlLocation>
     <sourceDestDir>src/main/java</sourceDestDir>
     <bindingDirectory>.</bindingDirectory>
     <bindingFiles>
       <bindingFile>jaxb/xsdbindings.xml</bindingFile>
       <bindingFile>jaxb/wsdlbindings.xml</bindingFile>
     </bindingFiles>
   </configuration>
</plugin>

Perhaps the plugin you are using has a similar option or perhaps you can consider switching.

也许您使用的插件有类似的选项,或者您可以考虑切换。

You can also provision your WSDL explicitly, in which case this property is ignored, though that may not be appropriate in a container-managed application.

您还可以显式地提供您的 WSDL,在这种情况下,该属性将被忽略,尽管这在容器管理的应用程序中可能不合适。

Sample code here.

示例代码在这里

回答by Dean Schulze

Version 1.12 doesn't recognize <wsdlLocation>. It complains:

1.12 版无法识别<wsdlLocation>. 它抱怨:

 No WSDLs are found to process, Specify atleast one of the following parameters: wsdlFiles, wsdlDirectory or wsdlUrls.

Version 1.9 (as in your example) just ignores everything and doesn't produce any code.

1.9 版(如您的示例中所示)只是忽略所有内容并且不生成任何代码。

Something must have changed.

一定有什么变了。

回答by Dean Schulze

Use wsdlLocationwith the jaxws-maven-plugin from org.jvnet.jax-ws-commons:

wsdlLocation与来自 org.jvnet.jax-ws-commons 的 jaxws-maven-plugin 一起使用:

<plugin>
<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>
    <wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
    <wsdlFiles>
        <wsdlFile>arsdev.wsdl</wsdlFile>
    </wsdlFiles>
    <wsdlLocation>wsdl/*</wsdlLocation>
    <!-- Keep generated files -->
    <keep>true</keep>
    <packageName>jaxws.remedy.client.generated</packageName>
    <!-- generated source files destination -->
    <sourceDestDir>target/generated-code/src</sourceDestDir>
</configuration>
</plugin>

回答by Guillaume Husta

I voted up for @dean-schulze answer, as it's appropriate for the case of org.jvnet.jax-ws-commons:jaxws-maven-pluginplugin.

我投票支持 @dean-schulze 答案,因为它适用于org.jvnet.jax-ws-commons:jaxws-maven-plugin插件的情况。

It may also be interesting to display help locally with CLI, like this :

使用 CLI 在本地显示帮助也可能很有趣,如下所示:

mvn jaxws:help -Dgoal=wsimport -Ddetail

As said in the previous answer, we can use the wsdlLocationparameter, described here :

正如前面的回答所说,我们可以使用wsdlLocation参数,描述如下:

wsdlLocation
  @WebService.wsdlLocation and @WebServiceClient.wsdlLocation value.
  Can end with asterisk in which case relative path of the WSDL will be
  appended to the given wsdlLocation.

  Example:

   ...
   <configuration>
   <wsdlDirectory>src/mywsdls</wsdlDirectory>
   <wsdlFiles>
   <wsdlFile>a.wsdl</wsdlFile>
   <wsdlFile>b/b.wsdl</wsdlFile>
   <wsdlFile>${basedir}/src/mywsdls/c.wsdl</wsdlFile>
   </wsdlFiles>
   <wsdlLocation>http://example.com/mywebservices/*</wsdlLocation>
   </configuration>
   ...
  wsdlLocation for a.wsdl will be http://example.com/mywebservices/a.wsdl
  wsdlLocation for b/b.wsdl will be
  http://example.com/mywebservices/b/b.wsdl
  wsdlLocation for ${basedir}/src/mywsdls/c.wsdl will be
  file://absolute/path/to/c.wsdl


  Note: External binding files cannot be used if asterisk notation is in
  place.

The -wsdllocationoption is also documented on the wsimportcommand from the JDK :

-wsdllocation选项也记录上的wsimport从JDK命令:

But it just says (see @WebServiceClient javadoc) :

但它只是说(见@WebServiceClient javadoc):

Specifies the @WebServiceClient.wsdlLocation value.