【问题标题】:Creating and using Webservices in same maven project在同一个 Maven 项目中创建和使用 Web 服务
【发布时间】:2011-06-25 10:41:55
【问题描述】:

我正在尝试构建一个 maven 项目,一个 OSGi 包,其中包含 Web 服务。我正在使用带有所有 @WebService 注释的 JAX-WS 来指定我拥有的 Web 服务。要在客户端位置加载这些 Web 服务,您通常使用 wsgenwsimport 来导出/导入 WSDL 文件。我打算使用jaxws-maven-plugin 这样做,但问题是:

捆绑包可以同时充当服务器和客户端。它可以将自己注册为同一捆绑包的父节点的客户端(在不同的 JVM/主机上运行)。所以这个 maven 项目/捆绑包为 web 服务定义了一个接口,并定义了一个实现这个接口的实现类。接口和类都照常使用@WebService注解。

@WebService
public interface Example {
    public void callMe();
}

@WebService
public class ExampleImpl implements Example {
    public void callMe() {};
}

然后在我的代码中的某个地方:

Endpoint p = Endpoint.publish(
                 "http://localhost:8080/example",
                 new ExampleImpl());    

jaxws:wsgen goal 读取注释并创建输出文件(.class 文件、.java 文件、WSDL 文件,具体取决于配置...)。但是我如何在jaxws:wsimport 目标期间使用这些文件来进行相同的mvn package 运行?在同一个maven项目中我想使用这个webservice,所以我需要写这样的东西:

ExampleImplService service = new ExampleImplService();
Example port = service.getExampleImplPort();
port.callMe();

jaxws:gen 目标在process-classes 阶段运行,因为它需要读取已编译的类,但jaxws:import 必须在generate-sources 阶段运行,以便为编译做好一切准备。现在我遇到了一个鸡蛋问题。我需要编译的类通过wsgen生成输出文件,但我需要wsgen的输出文件,用于maven的generate-sources阶段的wsimport。我的第一次尝试是将jaxws:wsgen 目标也分配给generate-sources 阶段,但由于类丢失/尚未编译,因此它当然不起作用。

我有什么办法来解决这个问题?我是否应该在generate-sources 阶段之前运行antrun 目标来编译一些 类(即只有带有@WebService 注释的类),以便jaxws:wsgen 可以使用它(在那个阶段),创建jaxws:wsimportgenerate-sources 阶段使用的输出文件?还有其他方法可以解决这个鸡蛋问题吗?在同一个 maven 项目中编译 web 服务的服务器和客户端部分是否还有其他“maven 方式”?它应该顺便说一句。从干净的mvn clean 构建运行,所以我不希望/不喜欢任何解决方案,例如“运行mvn package 两次以首先生成 web 服务文件,然后编译其他所有文件”。换句话说:mvn clean package 应该编译整个 maven 项目/osgi 包。

【问题讨论】:

  • 在您定义插件的地方,您必须设置两个单独的执行,一个用于 wsgen,另一个用于 wsimport。 ...时间流逝...stackoverflow.com/questions/2158175/…
  • 另一个 SO 问题中的解决方案假定同一项目中不需要“生成的存根”。然而,这正是我的项目中的情况,因此存根的生成不得晚于generate-source 阶段。

标签: java maven jax-ws wsimport wsgen


【解决方案1】:

我已经设法通过将jaxsw:wsgen 目标移至generate-sources 阶段来解决这个问题。我使用以下步骤。

  1. 首先我通过antrun 执行编译带有@WebService 注释的类,它使用<javac> 编译类。我将生成的 .class 文件保存在一个临时目录中,该目录在我创建客户端存根后被删除。
  2. 我使用jaxws:wsgen 目标从已编译的.class 文件创建WSDL 文件。
  3. 从临时目录中,我使用普通的jaxws:wsimport 目标创建客户端存根。
  4. 我通过第二次 antrun 执行删除了临时目录。

生成的 pom.xml 文件如下所示(仅相关部分)

<properties>
    <tmpdirectory>${java.io.tmpdir}${file.separator}${user.name}-${project.groupId}-${project.artifactId}</tmpdirectory>
</properties>
...
        <plugin>
            <!-- clean tmp directory at every "mvn clean" -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <filesets>
                    <fileset>
                        <directory>${tmpdirectory}</directory>
                    </fileset>
                </filesets>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <dependencies>
                  <dependency>
                      <groupId>com.sun</groupId>
                      <artifactId>tools</artifactId>
                      <version>1.6.0</version>
                      <scope>system</scope>
                      <systemPath>${java.home}/../lib/tools.jar</systemPath>
                  </dependency>
            </dependencies>  
            <executions>
                <execution>
                    <!-- compile webservice classes into tmp directory -->
                    <id>mini compile of webservices</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <property name="compile_classpath" refid="maven.compile.classpath"/>
                            <mkdir dir="${tmpdirectory}" />
                            <javac includeAntRuntime="false"
                                   classpath="${compile_classpath}"
                                   destdir="${tmpdirectory}">
                                <src path="${project.build.sourceDirectory}" />
                                <include name="org/example/project/*/webservice/*.java" />
                            </javac>
                        </target>
                    </configuration>
                </execution>
                <execution>
                    <!-- delete temporary directory (in case mvn clean is not called) -->
                    <id>clean up tmp dir</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <delete dir="${tmpdirectory}" />
                        </target>
                    </configuration>
                </execution>
            </executions>  
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>1.10</version>
            <executions>
                <execution>
                    <!-- generate WSDL file from the compiled classes in tmp directory -->
                    <id>generate wsdl file</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>wsgen</goal>
                    </goals>
                    <configuration>
                        <sei><!-- service endpoint implementation  --></sei>
                        <destDir>${tmpdirectory}</destDir>
                        <genWsdl>true</genWsdl>
                        <resourceDestDir>${tmpdirectory}</resourceDestDir>
                    </configuration>
                </execution>
                <execution>
                    <!-- create client stub files -->
                    <id>create client files from wsdl file</id>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <configuration>
                        <keep>true</keep>
                        <wsdlDirectory>${tmpdirectory}</wsdlDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

【讨论】:

    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2016-05-21
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多