jaxb,wsdl文件,重复的类

时间:2014-09-24 14:13:04

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

我有以下问题。我有项目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文件片段

<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 个答案:

答案 0 :(得分: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>