为什么使用 cxf-xjc-plugin 从 xsd 生成代码 (java) 在 jdk 11(亚马逊)上不起作用,但在 java 8 上起作用

Why generating code (java) from xsd with cxf-xjc-plugin doesn t work on jdk 11 (amazon), but did on java 8

我将项目从 Java 8 迁移到 Java 11,但在使用 cxf-xjc-plugin.

XSD 生成代码时遇到了一些问题

即使我确实将 cxf-xjc-plugin 升级到版本 3.3.0,但在构建项目时仍然遇到异常

我的pom.xml

       ...
           <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-xjc-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <extensions>
                        <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension>
                    </extensions>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xsdtojava</goal>
                        </goals>
                        <configuration>
                            <sourceRoot>${basedir}/target/generated-sources/xjc</sourceRoot>
                            <xsdOptions>
                                <xsdOption>
                                    <xsd>src/main/resources/xsd/leehwak.xsd</xsd>
                                    <packagename>fr.lee.hwak.batch.input</packagename>
                                    <extensionArgs>
                                        <arg>-encoding</arg>
                                        <arg>UTF-8</arg>
                                    </extensionArgs>
                                </xsdOption>
                            </xsdOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        ...

堆栈跟踪

[ERROR] C:\Users\Achraf\IdeaProjects\lee\lee-batch\target\null [-1:-1]: Failed to create model
com.sun.istack.SAXParseException2; Failed to create model
    at com.sun.tools.xjc.ErrorReceiver.error(ErrorReceiver.java:64)
    at org.apache.cxf.maven_plugin.XSDToJavaRunner.loadModel(XSDToJavaRunner.java:241)
    at org.apache.cxf.maven_plugin.XSDToJavaRunner.run(XSDToJavaRunner.java:177)
    at org.apache.cxf.maven_plugin.XSDToJavaRunner.main(XSDToJavaRunner.java:375)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.apache.cxf.maven_plugin.XSDToJavaRunner.loadModel(XSDToJavaRunner.java:239)
    ... 2 more
Caused by: java.lang.Error: java.lang.reflect.InvocationTargetException
    at com.sun.tools.xjc.reader.Ring.get(Ring.java:83)
    at com.sun.tools.xjc.reader.xmlschema.BGMBuilder.<init>(BGMBuilder.java:117)
    at com.sun.tools.xjc.reader.xmlschema.BGMBuilder.build(BGMBuilder.java:88)
    at com.sun.tools.xjc.ModelLoader.annotateXMLSchema(ModelLoader.java:366)
    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:121)
    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:76)
    ... 7 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.sun.tools.xjc.reader.Ring.get(Ring.java:72)
    ... 12 more
Caused by: java.lang.Error: java.lang.reflect.InvocationTargetException
    at com.sun.tools.xjc.reader.Ring.get(Ring.java:83)
    at com.sun.tools.xjc.reader.xmlschema.BindingComponent.getClassSelector(BindingComponent.java:35)
    at com.sun.tools.xjc.reader.xmlschema.ColorBinder.<init>(ColorBinder.java:32)
    at com.sun.tools.xjc.reader.xmlschema.BindGreen.<init>(BindGreen.java:33)
    ... 17 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.sun.tools.xjc.reader.Ring.get(Ring.java:72)
    ... 20 more
Caused by: java.lang.ExceptionInInitializerError
    at com.sun.tools.xjc.model.nav.NavigatorImpl.getBaseClass(NavigatorImpl.java:44)
    at com.sun.tools.xjc.model.nav.NavigatorImpl.getBaseClass(NavigatorImpl.java:29)
    at com.sun.xml.bind.v2.model.core.Adapter.<init>(Adapter.java:84)
    at com.sun.tools.xjc.model.CAdapter.<init>(CAdapter.java:52)
    at com.sun.tools.xjc.model.TypeUseFactory.adapt(TypeUseFactory.java:65)
    at com.sun.tools.xjc.model.CBuiltinLeafInfo.makeAdapted(CBuiltinLeafInfo.java:162)
    at com.sun.tools.xjc.model.CBuiltinLeafInfo.<clinit>(CBuiltinLeafInfo.java:358)
    at com.sun.tools.xjc.reader.xmlschema.SimpleTypeBuilder.<clinit>(SimpleTypeBuilder.java:867)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.sun.tools.xjc.reader.Ring.get(Ring.java:72)
    at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.<init>(DefaultClassBinder.java:68)
    at com.sun.tools.xjc.reader.xmlschema.ClassSelector.<init>(ClassSelector.java:184)
    ... 25 more
Caused by: java.lang.IllegalStateException: ReflectionNavigator.getInstance can't be found
    at com.sun.tools.xjc.model.nav.Utils.run(Utils.java:54)
    at com.sun.tools.xjc.model.nav.Utils.run(Utils.java:46)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at com.sun.tools.xjc.model.nav.Utils.<clinit>(Utils.java:45)
    ... 40 more


[ERROR] C:\Users\Achraf\IdeaProjects\lee\lee-batch\src\main\resources\xsd\leehwak.xsd [0:0]: Failed to create model

希望这对遇到同样问题的其他人有所帮助,

我基于这个 解决了这个问题,所以我最终像这样添加了 JAXB 依赖项:

...
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.8</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.2.8</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.8</version>
</dependency>
...

我的项目构建过时,但在运行时我收到一些警告:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/home/tra/.m2/repository/com/sun/xml/bind/jaxb-impl/2.3.0/jaxb-impl-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

并且我读到 这个问题将在 JAXB 的未来版本中得到解决