【问题标题】:jaxb, wsdl file, duplicated classesjaxb、wsdl 文件、重复的类
【发布时间】:2014-09-24 14:13:03
【问题描述】:

我有以下问题。我有项目 A,我有 common.xsd 文件,项目 B 依赖于 项目 A 并具有 main.xsd 文件。我使用剧集文件,我在 B 中的 pom 看起来像这样

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

然后通过

打开wsdl
<wsdl>true</wsdl> 

类已正确生成,但项目 A 中的这些公共类 是重复的。当我有 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文件的sn-p

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

【问题讨论】:

  • 我使用 this plugin 从 wsdl 生成类。我认为它是关于 wsdl 生成的“maven-jaxb2-plugin”的一个很好的替代品。
  • axis2(另一个 apache 项目)也很适合这个。它包含一个 wsdl2java 程序,它将生成所有存根类,供您将程序挂接到其中。 (虽然我认为 cxf 现在维护得更多了)
  • @Xstian,但是使用这个插件是否可以达到相同的结果。因为当我使用它时。它似乎忽略了我以前的选择。我收到错误,因为插件无法从 common.xsd 中找到类型
  • @Unyx 您可以使用这两个插件来实现我们的目标,“maven-jaxb2-plugin”从 xsd 生成类,“cxf-codegen-plugin”生成接口。第二个有一个参数可以通过命名空间排除每个类的生成(你应该放一个common.xsd的命名空间)。如果你想我添加一个“应该如何”的建议:)
  • 谢谢,我试试,我会发布结果

标签: java jaxb xsd wsdl maven-jaxb2-plugin


【解决方案1】:

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

请参阅&lt;extraarg&gt;-nexclude&lt;/extraarg&gt;&lt;extraarg&gt;NameSpaceOfxsd&lt;/extraarg&gt;,避免生成这些类。

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

【讨论】:

  • 应该是 common.xsd 或 main.xsd 的命名空间。 wsdl 文件导入 main.xsd ?我试图包含命名空间,但我仍然遇到未找到来自 common.xsd 的类型的相同错误
  • 在我的 wsdl 文件中,我使用来自 main.xsd 的类型定义。让我们成为A型。在我的复杂类型 typeA 的 main.xsd 定义中,我使用 common.xsd ( cmns:car) 中的类型,但出现错误,因为 cmns:car 无法解析
  • 你能添加一个你的wsdl的例子吗?为了更好地帮助你:)
  • 我更新了,我们可以继续讨论聊天吗?会更容易
猜你喜欢
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多