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

6tqwzwtp  于 2022-11-15  发布在  Maven
关注(0)|答案(5)|浏览(234)

我有一个JAX-WS驱动的Web服务,我们在另一个Maven模块(我将其称为ws-consumer)中从它的WSDL生成Web服务客户端。
无论好坏,我们都将“已发布的WSDL”(服务在发布时保存/生成的WSDL和XSD的版本)复制到ws-consumersrc/wsdl文件夹中,然后使用org.jvnet中的jaxws-maven-plugin生成一个使用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>

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

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

正如您希望看到的,wsdlLocation属性值具有一个硬编码的绝对路径,在部署服务时该路径将是不正确的。
有没有什么方法可以通过将其设置为META-INF/wsdl/MyWS/MyWS.wsdl或其他值来“控制”它?

rvpgvaaj

rvpgvaaj1#

将wsdlLocation与org.jvnet.jax-ws-commons中的jaxws-maven插件一起使用:

<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>
ki1q1bka

ki1q1bka2#

我投票赞成@dean-schulze的答案,因为它适合org.jvnet.jax-ws-commons:jaxws-maven-plugin插件的情况。
使用CLI在本地显示帮助可能也很有趣,如下所示:

mvn jaxws:help -Dgoal=wsimport -Ddetail

如前面的答案所述,我们可以使用 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.
  • -wsdllocation* 选项也记录在JDK的 wsimport 命令中:
  • JDK 7 -w导入
  • JDK 8 -w导入

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

Specifies the @WebServiceClient.wsdlLocation value.
k5ifujac

k5ifujac3#

@dean-schultze答案正确,只是没有更新以反映@jonashackt的注解,即wsdlLocation需要以斜杠作为前缀,以相对于基址。即

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <goals>
                <goal>wsimport</goal>
            </goals>
            <configuration>
                <wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
                <wsdlFiles>
                    <wsdlFile>some.wsdl</wsdlFile>
                </wsdlFiles>
                <wsdlLocation>/wsdl/*</wsdlLocation>
                <packageName>com.example.generated</packageName>
                <sourceDestDir>target/generated-sources/</sourceDestDir>
            </configuration>
        </execution>
    </executions>
</plugin>

也更新了groupId到codehaus。堆栈溢出不让我编辑原始的职位,试图纠正它

ndasle7k

ndasle7k4#

版本1.12无法识别<wsdlLocation>。它会抱怨:

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

版本1.9(如您的示例所示)只是忽略所有内容,不生成任何代码。
一定有什么改变了。

c0vxltue

c0vxltue5#

使用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>

也许你正在使用的插件有一个类似的选项,或者也许你可以考虑切换。
您还可以显式提供WSDL,在这种情况下,此属性将被忽略,但这可能不适用于容器管理的应用程序。

  • 此处为示例代码。*

相关问题