2014-09-24 95 views
2

我有以下問題。我有項目A,其中有common.xsd文件,項目B依賴於 項目A並具有main.xsd文件。我使用的片段文件,我的B中POM看起來像這樣jaxb,wsdl文件,重複類

<plugin> 
    <groupId>org.jvnet.jaxb2.maven2</groupId> 
    <artifactId>maven-jaxb2-plugin</artifactId> 
    <version>0.8.1</version> 
    <executions> 
     <execution> 
      <phase>generate-sources</phase> 
      <goals> 
       <goal>generate</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <args> 
      <arg>-Xannotate</arg> 
      <arg>-Xnamespace-prefix</arg> 
      <arg>-nv</arg> 
     </args> 
     <extension>true</extension> 
     <forceRegenerate>true</forceRegenerate> 
     <bindingDirectory>${basedir}/src/main/resources/xjb</bindingDirectory> 
     <bindingIncludes> 
      <include>*.xjb</include> 
     </bindingIncludes> 
     <schemas> 
      <schema> 
       <fileset> 
        <directory>${basedir}/src/main/resources/xsd/</directory> 
        <includes> 
         <include>B.xsd</include> 
        </includes> 
       </fileset> 
      </schema> 
      <schema> 
       <dependencyResource> 
        <groupId>AgroupID</groupId> 
        <artifactId>AartifactID</artifactId> 
        <resource>xsd/A.xsd</resource> 
       </dependencyResource> 
      </schema> 
     </schemas> 
     <episodes> 
      <episode> 
       <groupId>AgroupID</groupId> 
       <artifactId>AartifactID</artifactId> 
      </episode> 
     </episodes> 
     <debug>true</debug> 
     <verbose>true</verbose> 
     <plugins> 
      <plugin> 
       <groupId>org.jvnet.jaxb2_commons</groupId> 
       <artifactId>jaxb2-basics</artifactId> 
       <version>0.6.2</version> 
      </plugin> 
      <plugin> 
       <groupId>org.jvnet.jaxb2_commons</groupId> 
       <artifactId>jaxb2-basics-annotate</artifactId> 
       <version>0.6.2</version> 
      </plugin> 
      <plugin> 
       <groupId>org.jvnet.jaxb2_commons</groupId> 
       <artifactId>jaxb2-namespace-prefix</artifactId> 
       <version>1.1</version> 
      </plugin> 
     </plugins> 
    </configuration> 
</plugin> 

,但我也有WSDL文件,在那裏我有這個進口

<xsd:import namespace="SOME_NAMASPACE" schemaLocation="main.xsd" /> 

,當我改變

<include>B.xsd</include> 

<include>main.wsdl</include> 

and turn wsdl by

<wsdl>true</wsdl> 

類正確生成,但是從項目一 這些常見的類被複制。當我XSD的WSDL,而不是它的工作好

- 更新 -

<plugin> 
     <groupId>org.apache.cxf</groupId> 
     <artifactId>cxf-codegen-plugin</artifactId> 
     <executions> 
      <execution> 
       <id>generate-sources</id> 
       <phase>generate-sources</phase> 
       <configuration> 
        <excludes> 
         <exclude>classpath:xsd/common.xsd</exclude> 
        </excludes> 
        <sourceRoot>${generated_src}</sourceRoot> 
        <wsdlOptions> 
         <wsdlOption> 
          <wsdl>${project.basedir}PATH_TO_WSDL/main.wsdl</wsdl> 
         </wsdlOption> 
        </wsdlOptions> 
       </configuration> 

       <goals> 
        <goal>wsdl2java</goal> 
       </goals> 
      </execution> 
     </executions> 
     </plugin> 

- 更新 -

WSDL文件的片段

<wsdl:types> 
<xsd:schema 
    xmlns=... 
    targetNamespace=... 
    xmlns:carlic="NAMESPACE_OF_MAIN_XSD"> 
    <xsd:import namespace="NAMESPACE_OF_MAIN_XSD" schemaLocation="main.xsd" /> 

    <xsd:element name="carData" type="carlic:CarData" /> 
</xsd:schema> 
</wsdl:types> 
+1

我使用的這個插件](http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html),以從WSDL產生的類。我認爲這是一個很好的替代「maven-jaxb2-plugin」關於 wsdl世代。 – Xstian 2014-09-24 15:27:22

+1

也axis2(另一個Apache項目)是偉大的。包含一個wsdl2java程序,該程序將生成所有存根類,以便將程序掛接到該程序中。 (雖然我認爲cxf現在是一個更加維持一天) – SnakeDoc 2014-09-24 16:36:49

+0

@ Xstian,但是有可能使用這個插件實現相同的結果。因爲當我使用它。它似乎忽略了我以前的選擇。我得到的錯誤,因爲插件找不到來自common.xsd – Unyx 2014-09-25 07:25:59

回答

3

插件生成來自WSDL的接口。

<plugin> 
     <groupId>org.apache.cxf</groupId> 
     <artifactId>cxf-codegen-plugin</artifactId> 
     <version>2.7.3</version> 
     <executions> 
      <execution> 
       <id>generate-sources</id> 
       <phase>generate-sources</phase> 
       <configuration> 
        <wsdlOptions> 
         <wsdlOption> 
          <wsdl>${basedir}/src/main/resources/your.wsdl</wsdl> 
          <extraargs> 
           <extraarg>-nexclude</extraarg> 
           <extraarg>NameSpaceOfxsd</extraarg> 
          </extraargs> 
         </wsdlOption> 
        </wsdlOptions> 
       </configuration> 
       <goals> 
        <goal>wsdl2java</goal> 
       </goals> 
      </execution> 
     </executions> 
    </plugin> 

<extraarg>-nexclude</extraarg><extraarg>NameSpaceOfxsd</extraarg>能避免這些類的產生。

插件從XSD生成類。

 <plugin> 
      <groupId>org.jvnet.jaxb2.maven2</groupId> 
      <artifactId>maven-jaxb2-plugin</artifactId> 
      <version>0.8.1</version> 
      <executions> 
       <execution> 
        <phase>generate-sources</phase> 
        <goals> 
         <goal>generate</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <args> 
        <arg>-Xannotate</arg> 
        <arg>-Xnamespace-prefix</arg> 
        <arg>-nv</arg> 
       </args> 
       <extension>true</extension> 
       <forceRegenerate>true</forceRegenerate> 
       <bindingDirectory>${basedir}/src/main/resources/xjb</bindingDirectory> 
       <bindingIncludes> 
        <include>*.xjb</include> 
       </bindingIncludes> 
       <schemas> 
        <schema> 
         <fileset> 
          <directory>${basedir}/src/main/resources/</directory> 
          <includes> 
           <include>*.xsd</include> 
          </includes> 
         </fileset> 
        </schema> 
       </schemas> 
       <debug>true</debug> 
       <verbose>true</verbose> 
       <plugins> 
        <plugin> 
         <groupId>org.jvnet.jaxb2_commons</groupId> 
         <artifactId>jaxb2-basics</artifactId> 
         <version>0.6.2</version> 
        </plugin> 
        <plugin> 
         <groupId>org.jvnet.jaxb2_commons</groupId> 
         <artifactId>jaxb2-basics-annotate</artifactId> 
         <version>0.6.2</version> 
        </plugin> 
        <plugin> 
         <groupId>org.jvnet.jaxb2_commons</groupId> 
         <artifactId>jaxb2-namespace-prefix</artifactId> 
         <version>1.1</version> 
        </plugin> 
       </plugins> 
      </configuration> 
     </plugin> 

插件於每一類和接口添加到源代碼。

  <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>build-helper-maven-plugin</artifactId> 
      <version>1.1</version> 
      <executions> 
       <execution> 
        <id>add-source</id> 
        <phase>generate-sources</phase> 
        <goals> 
         <goal>add-source</goal> 
        </goals> 
        <configuration> 
         <sources> 
          <source>target/generated-sources/xjc</source> 
          <source>target/generated-sources/cxf</source> 
         </sources> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
+0

它應該是common.xsd或main.xsd的命名空間。 Wsdl文件導入main.xsd?我試圖包含命名空間,但是我仍然有同樣的錯誤,找不到來自common.xsd的類型 – Unyx 2014-09-25 10:36:27

+0

在我的wsdl文件中,我使用main.xsd中的類型difinition。設爲typeA。在複雜類型typeA的main.xsd定義中,我使用common.xsd(cmns:car)中的類型,並且出現錯誤,因爲cmns:car無法解析 – Unyx 2014-09-25 10:41:11

+0

您可以添加wsdl的示例嗎?爲了幫助你更好:) – Xstian 2014-09-25 10:55:35