3

I have the following problem. I have project A where I have common.xsd file, and project B which have dependency to project A and have main.xsd file. I use episode files and my pom in B looks like this

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

but I have also wsdl file where I have this import

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

and when I change

 <include>B.xsd</include>

to

<include>main.wsdl</include>

and turn wsdl by

<wsdl>true</wsdl> 

classes are generated properly, but these common classes from project A are duplicated. When I have xsd instead of wsdl it works good

-- update --

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

-- update --

snippet of wsdl file

<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>
Unyx
  • 97
  • 2
  • 8
  • 1
    I used [this plugin](http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html) to generate classes from wsdl. I think it's a good alternative to "maven-jaxb2-plugin" regarding wsdl generation. – Xstian Sep 24 '14 at 15:27
  • 1
    also axis2 (another apache project) is great for this. is includes a wsdl2java program which will generate all the stub classes for you to hook your program into. (although i think cxf is a little more maintained now-a-days) – SnakeDoc Sep 24 '14 at 16:36
  • @Xstian, but is it possible to achieve the same resultat using this plugin . because when I use it . it seems to ignore my previous options . I get error, because plugin can't find types from common.xsd – Unyx Sep 25 '14 at 07:25
  • @Unyx You could use both plugins to achieve our goal, "maven-jaxb2-plugin" to generate classes from xsd, and "cxf-codegen-plugin" to generate the interfaces. There is a argument in the second one that can exclude the generation of each class by namespace (you should put a namespace of common.xsd). If you want i add a proposal of "how should be" :) – Xstian Sep 25 '14 at 08:04
  • Thank you, I will try it and I'll post a result – Unyx Sep 25 '14 at 08:08
  • unfortunately it doesn't work or I do something wrong. I updated my question on the top – Unyx Sep 25 '14 at 08:38
  • Please give me some time, I'll get back to you to make sure it works. I'm moving `maven-jaxb2-plugin` to GitHub, you can send me a PR with your test case there. I'll get back to you. – lexicore Sep 25 '14 at 08:59
  • @Xstian, Could you write this proposal? lexicore, I can send you PR today , but how long will you take to code this functionality ? – Unyx Sep 25 '14 at 09:45
  • @Unyx: Please your test case here and send me the PR: https://github.com/highsource/maven-jaxb2-plugin/tree/master/tests If this is a problem in `maven-jaxb2-plugin` or a configuration, I'll do it fast. If it is a problem in XJC then I can't promise anything as it is Oracle's thing. – lexicore Sep 25 '14 at 19:32

1 Answers1

3

Plugin to generate interfaces from 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>

See <extraarg>-nexclude</extraarg><extraarg>NameSpaceOfxsd</extraarg> that avoid the generation of these classes.

Plugin to generate classes from 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 to add each class and interface to source.

         <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>
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • it should be namespace of common.xsd or main.xsd . Wsdl file import main.xsd ? I tried to include namespace, but I still have the same error that types from common.xsd are not found – Unyx Sep 25 '14 at 10:36
  • In my wsdl file I use type difinition from main.xsd . Let is be typeA. In my main.xsd definition of complex type typeA I use type from common.xsd ( cmns:car) and I get error, because cmns:car can't be resolved – Unyx Sep 25 '14 at 10:41
  • Can you add an example of your wsdl? in order to help you better :) – Xstian Sep 25 '14 at 10:55
  • I updated, Could we continue discussion on chat ? It'll be easier – Unyx Sep 25 '14 at 11:04
  • http://chat.stackoverflow.com/rooms/61901/jaxb-wsdl-file-duplicated-classes-4-unyx – Xstian Sep 25 '14 at 12:27
  • what if the issue is nested classes and we can't exclude them – dtc May 25 '16 at 21:31
  • @dtc You could use external binding to refer an existing class (centralized) – Xstian May 26 '16 at 07:16
  • @Xstian thanks! i figured it out with "jaxb:class name" binding. didn't know how flexible that tool was. – dtc May 26 '16 at 16:42