2011-02-11 61 views
2

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

该软件包可以同时充当服务器和客户端。它可以将自己注册为同一捆绑包的父节点(运行在不同的JVM /主机上)的客户端。所以这个maven项目/ bundle为webservice定义了一个接口并定义了实现这个接口的实现类。像往常一样,接口和类都使用@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项目我想用这个Web服务,所以我需要写的东西是这样的:

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

jaxws:gen目标是在process-classes阶段运行,因为它需要读取编译的类,但jaxws:import必须运行在generate-sources阶段,准备编译所有内容。现在我遇到了鸡蛋问题。我需要编译的类通过wsgen生成输出文件,但是我需要wsgen的输出文件在阶段的阶段maven的wsimport。我的第一次尝试是将jaxws:wsgen目标分配到generate-sources阶段,但当然由于这些类缺少/尚未编译,因此它不起作用。

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

回答

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

在您定义插件的地方,您将不得不设置两个单独的执行,一个用于wsgen和另一个wsimport。

...久而久之...

Use Maven to trigger a wsgen & wsimport in a row, using wsdlLocation

+0

在其他溶液中,以质疑认为不需要在同一个项目中的“生成存根”。然而,在我的项目中情况正是如此,所以存根不得晚于`generate-source`阶段生成。 – Progman 2011-02-15 08:02:20