Search code examples
mavensoapwsdlsoap-clientmaven-jaxb2-plugin

Cannot generate source files from XSD files using Maven?


I have this wsdl schema. I need to generate source code files from that WSDL schema. I tried many many different things, but couldn't get to generate Java stubs. In this post I'll try to outline only the last and most relevant approach I took. Also, I will be pointing out my suspicions where might be the problem and I'll post console error I received. There is the link to the GitHub repo(It is simple Maven project, in Spring Tools Suite, with only one starter class and no settings what so ever. I used Java 1.8).

Approach

My very first attempt was to use SOAP UI tool, and from link above generated *.xsd files using that tool. After successfully created those files, I placed them in src/main/resources/arvatoprod folder within Maven project. Then I created following pom.xml (omitted irrelevant dependencies) within Maven project.

    <build>
        <finalName>Fleaty</finalName>
        <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>
                    </execution>
                </executions>
                <configuration>
                    <generatePackage>com.zesium.arvato</generatePackage>
                    <schemaDirectory>src/main/resources/arvatoprod</schemaDirectory>
                    <schemaIncludes>
                        <include>*.xsd</include>
                    </schemaIncludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

Before that I actually, as I always do, did Maven-clean command. Messages I got from the console, after Maven generate-sources command, are as follows (I'll paste entire message and explain my take on key lines):

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/C:/Users/Miljan/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.0/jaxb-runtime-2.3.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[ERROR] Error while generating code.Location [ file:/C:/Users/Miljan/Desktop/CommitRevertGeneratedFromXsdWorkspaceArvoProd/Fleaty/src/main/resources/arvatoprod/AFSService_1.xsd{377,94}].
com.sun.istack.SAXParseException2; systemId: file:/C:/Users/Miljan/Desktop/CommitRevertGeneratedFromXsdWorkspaceArvoProd/Fleaty/src/main/resources/arvatoprod/AFSService_1.xsd; lineNumber: 377; columnNumber: 94; 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:957)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
[ERROR] Error while generating code.Location [ file:/C:/Users/Miljan/Desktop/CommitRevertGeneratedFromXsdWorkspaceArvoProd/Fleaty/src/main/resources/arvatoprod/AFSService_1.xsd{328,106}].
com.sun.istack.SAXParseException2; systemId: file:/C:/Users/Miljan/Desktop/CommitRevertGeneratedFromXsdWorkspaceArvoProd/Fleaty/src/main/resources/arvatoprod/AFSService_1.xsd; lineNumber: 328; columnNumber: 106; (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:957)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.520 s
[INFO] Finished at: 2021-01-22T20:08:48+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.14.0:generate (default) on project Fleaty: Failed to compile input schema(s)! Error messages should have been provided. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

First section of error above:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/C:/Users/Miljan/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.0/jaxb-runtime-2.3.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

I'm not exactly sure what this message is supposed to tell me. In any case, I've tried deleting the folder 2.3.0 at the location /C:/Users/Miljan/.m2/repository/org/glassfish/jaxb/jaxb-runtime/ since on that location I have got bunch of other versions/folders 2.3.1, 2.3.4. Every time I hit Maven generate-sources, that folder 2.3.0 reappears. The content of that folder can be se on the image bellow:

Content of jaxb 2.3.0

Second section of the error above

The message above also contains these four lines:

ERROR] Error while generating code.Location [ file:/C:/Users/Miljan/Desktop/CommitRevertGeneratedFromXsdWorkspaceArvoProd/Fleaty/src/main/resources/arvatoprod/AFSService_1.xsd{377,94}].
com.sun.istack.SAXParseException2; systemId: file:/C:/Users/Miljan/Desktop/CommitRevertGeneratedFromXsdWorkspaceArvoProd/Fleaty/src/main/resources/arvatoprod/AFSService_1.xsd; lineNumber: 377; columnNumber: 94; Two declarations cause a collision in the ObjectFactory class.

and

[ERROR] Error while generating code.Location [ file:/C:/Users/Miljan/Desktop/CommitRevertGeneratedFromXsdWorkspaceArvoProd/Fleaty/src/main/resources/arvatoprod/AFSService_1.xsd{328,106}].
com.sun.istack.SAXParseException2; systemId: file:/C:/Users/Miljan/Desktop/CommitRevertGeneratedFromXsdWorkspaceArvoProd/Fleaty/src/main/resources/arvatoprod/AFSService_1.xsd; lineNumber: 328; columnNumber: 106; (Related to above error) This is the other declaration.

So the wsimport tool (which is probably use by jax2b-maven-plugin to generate stubs) cannot build those Java classes, since there is some kind of naming error. If I'm correct, essentially this is compile error and for some reason wsimport tool (or the plugin) cannot resolve it.

I was not lazy, so I opened the xsd file which is referred in the error, AFSService_1.xsd. This is how lines 328 and 377 look like:

Line 328:

  <xs:element minOccurs="0" name="AccountInfo" nillable="true" type="tns:AFSInvoiceAccountInfo"/>

Line 377:

  <xs:element name="AFSInvoiceAccountInfo" nillable="true" type="tns:AFSInvoiceAccountInfo"/>

I did my research and what people are doing something called binding (docs). Essentially, it is possible to change the naming of a method, data type etc. Now, I'm not super well versed with XSD, WSDLs and generally with JAXB architecture. I do know the basic terminology (what are xml namespaces, xsd schemas, what are SAX and DOM parsers, XPath, XQuery, I used all of those).

What I'm not sure is whether I'm supposed to write those bindings, that is my last step to take, or I just need to configure something on my machine/project to make those stubs?


Solution

  • Thanks for providing the full picture in the question! The following answer is pretty much the one given here: https://stackoverflow.com/a/13438253/618156

    The issue is mainly due to XJC having trouble with AccountInfo and AFSInvoiceAccountInfo (according to the answer linked above).

    So it seems that one needs to discern them manually:

    You need the following bindings.xjb file in src/main/resources (will be found automatically by the maven-xjb2-plugin):

    <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
                   xmlns:xs="http://www.w3.org/2001/XMLSchema"
                   version="1.0">
        <jaxb:bindings schemaLocation="https://clienttesthorizon.horizonafs.com/AFSServices/AFSService.svc?xsd=xsd2">
            <jaxb:bindings node="//xs:element[@name='AFSInvoiceAccountInfo']">
                <jaxb:factoryMethod name="AFSInvoiceAccountInfo"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='AccountInfo']">
                <jaxb:factoryMethod name="AccountInfo"/>
            </jaxb:bindings>
        </jaxb:bindings>
    </jaxb:bindings>
    

    ... which worked for me with the following configuration for the maven-xjb2-plugin in your pom.xml (removed everything that I deemed unnecessary for my testing, it's pretty close to what you originally posted in the question):

    <project...>
        <!-- ... -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <version>0.14.0</version>
                    <configuration>
                        <schemaLanguage>WSDL</schemaLanguage>
                        <schemas>
                            <schema>
                                <url>https://clienttesthorizon.horizonafs.com/AFSServices/AFSService.svc?wsdl</url>
                            </schema>
                        </schemas>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- ... -->
            </plugins>
        </build>
    </project>