gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:44:18 26 4
gpt4 key购买 nike

我有这个wsdl schema .我需要从该 WSDL 模式生成源代码文件。我尝试了许多不同的东西,但无法生成 Java stub 。在这篇文章中,我将尝试仅概述我采用的最后一种也是最相关的方法。另外,我会指出我的怀疑可能是问题所在,我会发布我收到的控制台错误。有指向 GitHub repo 的链接(这是一个简单的 Maven 项目,在 Spring Tools Suite 中,只有一个入门类,没有任何设置。我使用的是 Java 1.8)。
联系方式
我的第一次尝试是使用 SOAP UI工具,并从上面的链接生成*.xsd使用该工具的文件。成功创建这些文件后,我将它们放在 src/main/resources/arvatoprod Maven 项目中的文件夹。然后我创建了 pom.xml (省略不相关的依赖项)在 Maven 项目中。

    <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
我不确定这条消息应该告诉我什么。无论如何,我已经尝试删除文件夹 2.3.0在位置 /C:/Users/Miljan/.m2/repository/org/glassfish/jaxb/jaxb-runtime/因为在那个位置我有一堆其他版本/文件夹 2.3.1 , 2.3.4 .每次点击 Maven generate-sources ,那个文件夹 2.3.0再次出现。该文件夹的内容可以在下面的图像中找到:
Content of jaxb 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 使用它来生成 stub )无法构建这些 Java 类,因为存在某种命名错误。如果我是正确的,本质上这是编译错误,出于某种原因 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)。本质上,可以更改方法、数据类型等的命名。现在,我对 XSD 不是很精通。 , WSDL s 并且通常使用 JAXB建筑学。我确实知道基本术语(什么是 xml namespacesxsd schemas ,什么是 SAXDOM 解析器, XPathXQuery ,我用过所有这些)。
我不确定我是否应该编写这些绑定(bind),这是我要采取的最后一步,还是我只需要在我的机器/项目上配置一些东西来制作这些 stub ?

最佳答案

感谢您在问题中提供完整图片!
以下答案几乎就是这里给出的答案:https://stackoverflow.com/a/13438253/618156
该问题主要是由于 XJC 遇到 AccountInfo 问题。和 AFSInvoiceAccountInfo (根据上面链接的答案)。
因此,似乎需要手动识别它们:
您需要以下 bindings.xjb文件在 src/main/resources (将由 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>

关于maven - 无法使用 Maven 从 XSD 文件生成源文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63807268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com