无法使用 Maven 从 XSD 个文件生成源文件?

Cannot generate source files from XSD files using Maven?

我有这个 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(这是一个简单的 Maven 项目,在 Spring Tools Suite 中,只有一个启动器 class 并且没有任何设置。我使用 Java 1.8).

接近

我的第一次尝试是使用 SOAP UI 工具,并使用该工具从上面的 link 生成了 *.xsd 文件。成功创建这些文件后,我将它们放在 Maven 项目中的 src/main/resources/arvatoprod 文件夹中。然后我在 Maven 项目中创建了以下 pom.xml(省略了不相关的依赖项)。

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

在那之前,我实际上一如既往地执行了 Maven-clean 命令。在 Maven generate-sources 命令之后,我从控制台收到的消息如下(我将粘贴整个消息并解释我对关键行的看法):

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

上面第一段错误:

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

我不太确定这条消息要告诉我什么。无论如何,我都尝试删除 /C:/Users/Miljan/.m2/repository/org/glassfish/jaxb/jaxb-runtime/ 位置的文件夹 2.3.0,因为在那个位置我有一堆其他 versions/folders 2.3.12.3.4 .每次我点击 Maven generate-sources 时,该文件夹 2.3.0 都会重新出现。该文件夹的内容可以在下面的图像中看到:

上面第二段错误

上面的消息也包含这四行:

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.

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

因此 wsimport 工具(可能被 jax2b-maven-plugin 用于生成存根)无法构建那些 Java classes,因为存在某种命名错误。如果我是对的,本质上这是编译错误,由于某种原因 wsimport 工具(或插件)无法解决它。

我并不懒惰,所以我打开了错误中引用的xsd文件,AFSService_1.xsd。这是 328377 行的样子:

328行:

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

377行:

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

我做了我的研究,人们正在做一些叫做 binding (docs) 的事情。本质上,可以更改方法的命名、数据类型等。现在,我不是很精通 XSDWSDLs 和 JAXB 体系结构。我确实知道基本术语(什么是 xml namespacesxsd schemas、什么是 SAXDOM 解析器、XPathXQuery,我用过所有这些)。

我不确定我是否应该编写这些绑定,这是我要执行的最后一步,或者我只需要在我的 machine/project 上配置一些东西来制作这些存根?

感谢您提供问题的全貌! 以下答案几乎就是此处给出的答案:

这个问题主要是由于 XJC 在 AccountInfoAFSInvoiceAccountInfo 上有问题(根据上面链接的答案)。

看来需要手动辨别了:

您需要 src/main/resources 中的以下 bindings.xjb 文件(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>

... 这对我有用 maven-xjb2-plugin 在您的 pom.xml 中的以下配置(删除了我认为对我的测试不必要的所有内容,它与您最初发布的内容非常接近问题):

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