maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

Tags: , , ,



I have created .wsdl file in /src/main/resources/wsdl. Here I have pasted the the content of the soap wsdl.

I added the following plugin to maven:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.14.0</version>

        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>

                <configuration>
                    <generatePackage>bankup.wsdl</generatePackage>
                    <generateDirectory>${project.basedir}/src/main/java</generateDirectory>
                    <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
                    <schemaIncludes>
                        <include>*.wsdl</include>
                    </schemaIncludes>
                    <clearOutputDir>true</clearOutputDir>
                </configuration>

            </execution>

        </executions>
    </plugin>

The project fails to build due to the error in generating the classes from the wsdl. There are two declarations collesion in the objectFactory class. The error is like below:

[ERROR] Error while generating code.Location [ http://uri/ifx?xsd=ws-bankup_schema4.xsd{3125,63}].
com.sun.istack.SAXParseException2: Two declarations cause a collision in the ObjectFactory class.
    at com.sun.tools.xjc.ErrorReceiver.error (ErrorReceiver.java:86)
    at com.sun.tools.xjc.generator.bean.ObjectFactoryGeneratorImpl.populate (ObjectFactoryGeneratorImpl.java:191)
    at com.sun.tools.xjc.generator.bean.PublicObjectFactoryGenerator.populate (PublicObjectFactoryGenerator.java:59)
    at com.sun.tools.xjc.generator.bean.BeanGenerator.<init> (BeanGenerator.java:272)
    at com.sun.tools.xjc.generator.bean.BeanGenerator.generate (BeanGenerator.java:171)
    at com.sun.tools.xjc.model.Model.generateCode (Model.java:288)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.generateCode (XJC23Mojo.java:66)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute (XJC23Mojo.java:41)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute (XJC23Mojo.java:28)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute (RawXJC2Mojo.java:478)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute (RawXJC2Mojo.java:320)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[ERROR] Error while generating code.Location [ http://uri/ifx?xsd=ws-bankup_schema5.xsd{3,63}].
com.sun.istack.SAXParseException2: (Related to above error) This is the other declaration.   
    at com.sun.tools.xjc.ErrorReceiver.error (ErrorReceiver.java:86)
    at com.sun.tools.xjc.generator.bean.ObjectFactoryGeneratorImpl.populate (ObjectFactoryGeneratorImpl.java:193)
    at com.sun.tools.xjc.generator.bean.PublicObjectFactoryGenerator.populate (PublicObjectFactoryGenerator.java:59)
    at com.sun.tools.xjc.generator.bean.BeanGenerator.<init> (BeanGenerator.java:272)
    at com.sun.tools.xjc.generator.bean.BeanGenerator.generate (BeanGenerator.java:171)
    at com.sun.tools.xjc.model.Model.generateCode (Model.java:288)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.generateCode (XJC23Mojo.java:66)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute (XJC23Mojo.java:41)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute (XJC23Mojo.java:28)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute (RawXJC2Mojo.java:478)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute (RawXJC2Mojo.java:320)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

From the reading the log, I understand that wsdl has many schemes that have collision.

I checked the schemes and saw that schema6 imports schema4, and schema4 import schema3, and schema3 imports schema2, and schema2 imports schema1. To prevent the collision I changed the plugin configuration to create schema objects in separated packages. Below it modification i did to plugin:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <executions>

        <execution>
            <id>schema6-genearate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaDirectory>src/main/resources/wsdl</schemaDirectory>
                <schemaIncludes>
                    <include>schema6.xsd</include>
                </schemaIncludes>
                <generatePackage>bankup.schema6.generated</generatePackage>
                <generateDirectory>${project.build.directory}/generated-sources/xjc1</generateDirectory>
            </configuration>
        </execution>

        <execution>
            <id>schema5-genearate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaDirectory>src/main/resources/wsdl</schemaDirectory>
                <schemaIncludes>
                    <include>schema5.xsd</include>
                </schemaIncludes>
                <generatePackage>bankup.schema5.generated</generatePackage>
                <generateDirectory>${project.build.directory}/generated-sources/xjc1</generateDirectory>
            </configuration>
        </execution>

    </executions>
</plugin>

Even though they are being generated in different packages, it still gives me the same error.

[ERROR] Error while generating code.Location [ schema4.xsd{3125,63}].
com.sun.istack.SAXParseException2: Two declarations cause a collision in the ObjectFactory class.
    at com.sun.tools.xjc.ErrorReceiver.error (ErrorReceiver.java:86)
    at com.sun.tools.xjc.generator.bean.ObjectFactoryGeneratorImpl.populate (ObjectFactoryGeneratorImpl.java:191)
    at com.sun.tools.xjc.generator.bean.PublicObjectFactoryGenerator.populate (PublicObjectFactoryGenerator.java:59)
    at com.sun.tools.xjc.generator.bean.BeanGenerator.<init> (BeanGenerator.java:272)
    at com.sun.tools.xjc.generator.bean.BeanGenerator.generate (BeanGenerator.java:171)
    at com.sun.tools.xjc.model.Model.generateCode (Model.java:288)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.generateCode (XJC23Mojo.java:66)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute (XJC23Mojo.java:41)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute (XJC23Mojo.java:28)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute (RawXJC2Mojo.java:478)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute (RawXJC2Mojo.java:320)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[ERROR] Error while generating code.Location [schema5.xsd{3,63}].
com.sun.istack.SAXParseException2: (Related to above error) This is the other declaration.   

Why is this still happening?? I will appreciate any help, by tomorrow I will put a boundty of 50+ points plus who help me solve this problem.

Thank you.

Answer

To fix this issue, you need to create bind.xjb file. Create the bind.xjb file in the path src/main/java/bindings. In this file we will solve the conflicts that prevent the creation of ObjectFactory. What is happining is that in you schema you have the ObjectFactory declared in schema6 and schema5. So a workaround to this would that this schema to be generated in separated packages. In the bind.xjb file, we will instruct the maven plugin to generate the classes from schema5 and schema6 in different packages. Below it is the code for bind.xjb file:

<jaxb:bindings version="1.0"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc">

    <jaxb:bindings
        schemaLocation="schema6.xsd"
        node="/xs:schema">

        <jaxb:schemaBindings>
            <jaxb:package name="bankup.schema6" />
        </jaxb:schemaBindings>

    </jaxb:bindings>

    <jaxb:bindings
        schemaLocation="schema5.xsd"
        node="/xs:schema">

        <jaxb:schemaBindings>
            <jaxb:package name="bankup.schema5" />
        </jaxb:schemaBindings>

    </jaxb:bindings>

</jaxb:bindings>

After that you need to configure bind.xjb in maven jaxb2 plugin. To do that, you should modify the maven jaxb2 plugin like below:

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>

                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>

                        <configuration>
                            <bindingDirectory>${project.basedir}/src/main/resources/bindings</bindingDirectory>
                            <bindingIncludes>
                                <include>bind.xjb</include>
                            </bindingIncludes>
                            <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
                            <schemaIncludes>
                                <include>*.wsdl</include>
                            </schemaIncludes>
                            <clearOutputDir>true</clearOutputDir>
                            <vmArgs>
                                <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
                            </vmArgs>
                        </configuration>

                    </execution>


                </executions>

            </plugin>

After running the build, all the classes should be generated in the default directory: target/generated-sources/xjc.

Hope this is helpful to you.



Source: stackoverflow