2013-04-09 32 views
14

我在配置jaxb2-maven-plugin以從WSDL生成Java類和存在於同一個標準目錄src/main/xsd中的多個XSD文件時遇到問題。使用來自WSDL的jaxb2-maven-plugin生成類

how to use jaxb2 maven plugin with inline XSD?僅與答案正確建議在插件配置中使用wsdl參數相關,但該問題確實涉及內聯XSD和我的XSD是外部的。

插件目標參數列表here

我的插件配置爲:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>jaxb2-maven-plugin</artifactId> 
    <version>1.5</version> 
    <executions> 
     <execution> 
      <id>xjc</id> 
      <goals> 
       <goal>xjc</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <packageName>com.x.y.model</packageName> 
     <wsdl>true</wsdl> 
    </configuration> 
</plugin> 

我與mvn -X clean jaxb2:xjc測試這一點,但該插件忽略.wsdl作爲調試輸出

[DEBUG] accept false for file c:\projects\foo\src\main\xsd\service.wsdl 
[DEBUG] accept true for file c:\projects\foo\src\main\xsd\datatypes.xsd 
[DEBUG] accept true for file c:\projects\foo\src\main\xsd\more-datatypes.xsd 

回答

21

看到通過檢查的Maven的調試輸出傳遞給JAXB XJC的參數(以及一些試驗和錯誤)我發現我需要爲插件提供2個以上的配置參數。

這將停止插件掃描XSD文件,並僅使用.wsdl作爲源。例如,XSD文件作爲<xsd:include schemaLocation="datatypes.xsd" />指令包含在WSDL中,這些指令在本地解決,導致WSDL和XSD中的所有類型都作爲Java類生成。

爲我工作的部分配置是:

<configuration> 
    <packageName>com.x.y.model</packageName> 
    <wsdl>true</wsdl> 
    <xmlschema>false</xmlschema> 
    <schemaFiles>service.wsdl</schemaFiles> 
</configuration> 

沒有與<xmlschema>false</xmlschema> Maven的錯誤:

org.apache.maven.lifecycle.LifecycleExecutionException:未能執行目標org.codehaus .mojo:jaxb2-maven-plugin:1.5:項目foo上的xjc(default-cli):無法處理架構: /c:/projects/foo/src/main/xsd/service.wsdl

+2

OMG,謝謝你這作品時,我不指定目錄,並使用默認的XSD目錄 – Erich 2013-08-22 00:10:04

+0

謝謝,我需要的! – evandongen 2013-10-25 10:29:13

3

我試圖jaxb2-maven-plugin其生成的Java文件

<plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>jaxb2-maven-plugin</artifactId> 
       <executions> 
        <execution> 
         <goals> 
          <goal>xjc</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <schemaDirectory>src/main/webapp/schemas/</schemaDirectory> 
        <wsdl>true</wsdl> 
        <outputDirectory>src/main/java</outputDirectory> 
       </configuration> 
      </plugin> 

要運行這個我已經使用命令

mvn jaxb2:xjc 

試試這個它會生成JAXB類到你的src文件夾。希望你正在尋找這個。

5

如果您正在生成wsdl和xsd以及嘗試放入不同的執行配置:它可能不具有相同的schemaDirectory或插件將無法成功運行第二次執行,導致它基於此變量緩存執行。我建議這樣做,如

 <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>jaxb2-maven-plugin</artifactId> 
      <version>1.6</version> 
      <executions> 
       <execution> 
        <id>generate-sri-facturas</id> 
        <phase>generate-sources</phase> 
        <goals> 
         <goal>xjc</goal> 
        </goals> 
        <configuration> 
         <outputDirectory>target/generated-sources/sri</outputDirectory> 
         <packageName>${commonsource.packageName}</packageName> 
         <schemaDirectory>src/main/resources/schema/xsd</schemaDirectory> 
         <schemaFiles>factura_v1.1.0.xsd</schemaFiles> 
        </configuration> 
       </execution> 
       <execution> 
        <id>generate-sri-autorizacion-comprobantes</id> 
        <phase>generate-sources</phase> 
        <goals> 
         <goal>xjc</goal> 
        </goals> 
        <configuration> 
         <outputDirectory>target/generated-sources/sri/autorizacion</outputDirectory> 
         <packageName>${commonsource.packageName}.autorizacion</packageName> 
         <wsdl>true</wsdl> 
         <xmlschema>false</xmlschema> 
         <schemaDirectory>src/main/resources/schema/wsdl</schemaDirectory> 
         <schemaFiles>AutorizacionComprobantes.wsdl</schemaFiles> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 

我創建了一個xsdwsdl文件夾分別進行配置。

0

您可以使用的配置如下代碼:

   <configuration> 
        <!-- Package to store the generated file --> 
        <packageName>com.example.demo.wsdl</packageName> 
        <!-- Treat the input as WSDL --> 
        <wsdl>true</wsdl> 
        <!-- Input is not XML schema --> 
        <xmlschema>false</xmlschema> 
        <!-- The WSDL file that you saved earlier --> 
        <schemaFiles>horarios.wsdl</schemaFiles> 
        <!-- The location of the WSDL file --> 
        <schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory> 
        <!-- The output directory to store the generated Java files --> 
        <outputDirectory>${project.basedir}/src/main/java</outputDirectory> 
        <!-- Don't clear output directory on each run --> 
        <clearOutputDir>false</clearOutputDir> 
       </configuration>