2011-02-11 57 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