【问题标题】:Generate classes with jaxb2-maven-plugin from WSDL从 WSDL 使用 jaxb2-maven-plugin 生成类
【发布时间】:2013-03-31 23:02:58
【问题描述】:

我在配置 jaxb2-maven-plugin 以从 WSDL 和多个 XSD 文件生成 Java 类时遇到问题,这些文件都存在于同一标准目录 src/main/xsd 中。

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

【问题讨论】:

    标签: java maven jaxb jaxb2-maven-plugin


    【解决方案1】:

    对于 2.5 版,配置如下:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>2.5.0</version>
        <executions>
            <execution>
                <id>xjc</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>xjc</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                    <packageName>com.packagename</packageName>
                    <sources>
                        <source>${project.basedir}/src/main/resources/wsdl</source>
                    </sources>
                    <sourceType>wsdl</sourceType>
                    <clearOutputDir>false</clearOutputDir>
                    <noPackageLevelAnnotations>true</noPackageLevelAnnotations>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

      【解决方案2】:

      我对 jaxb2-maven-plugin 2.5.0 有这个问题。这是我的解决方案:

      <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>jaxb2-maven-plugin</artifactId>
          <version>2.5.0</version>
          <executions>
              <execution>
                  <phase>generate-resources</phase>
                  <id>xjc</id>
                  <goals>
                      <goal>xjc</goal>
                  </goals>
              </execution>
          </executions>
          <configuration>
              <sourceType>wsdl</sourceType>
              <sources>
                  <source>${project.basedir}/src/main/resources/wsdl</source>
              </sources>
              <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
              <clearOutputDir>false</clearOutputDir>
              <packageName>com.project.generated</packageName>
              <noPackageLevelAnnotations>true</noPackageLevelAnnotations>
          </configuration>
      </plugin>
      

      【讨论】:

      • 配置错误,配置标签​​必须在执行标签内。
      【解决方案3】:

      您可以在配置中使用以下代码:

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

      【讨论】:

        【解决方案4】:

        如果您同时生成 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> 
        

        我创建了一个xsd 和一个wsdl 文件夹来分隔配置。

        【讨论】:

          【解决方案5】:

          我试过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
          

          试试这个,它会在你的 src 文件夹中生成 jaxb 类。希望你正在寻找这个。

          【讨论】:

            【解决方案6】:

            通过检查传递给 JAXB XJC 的参数的 Maven 调试输出(以及一些试验和错误),我发现我需要为插件提供另外 2 个配置参数。

            这将停止插件对 XSD 文件的扫描,并仅使用 .wsdl 作为源。例如,XSD 文件包含在 WSDL as&lt;xsd:include schemaLocation="datatypes.xsd" /&gt; 指令中,这些指令在本地解析,导致 WSDL 和 XSD 中的所有类型都生成为 Java 类。

            对我有用的配置部分是:

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

            没有&lt;xmlschema&gt;false&lt;/xmlschema&gt; Maven 错误:

            org.apache.maven.lifecycle.LifecycleExecutionException: 无法在项目 foo 上执行目标 org.codehaus.mojo:jaxb2-maven-plugin:1.5:xjc (default-cli):无法处理架构: /c:/projects/foo/src/main/xsd/service.wsdl

            【讨论】:

            • omg,谢谢你,当我不指定目录并使用默认的 xsd 目录时,这有效
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-30
            • 1970-01-01
            • 2013-12-18
            相关资源
            最近更新 更多