由于“[..]ToStringStrategy 无法转换为 [..]ToStringStrategy2”,JAXB2 无法为 XSD 生成 类

JAXB2 cannot generate classes for XSD due to '[..]ToStringStrategy cannot be converted to [..]ToStringStrategy2'

我发现将 maven-jaxb2-plugin 与版本 0.12.0 的 org.jvnet.jaxb2_commons.jaxb2-basics 一起使用时,toString 生成存在问题。它没有生成 JAXBToStringStrategy.INSTANCE2,而是使用与 ToStringStrategy2 不兼容的 INSTANCE,如下所示:

public String toString() {
    final ToStringStrategy2 strategy = JAXBToStringStrategy.INSTANCE;
    final StringBuilder buffer = new StringBuilder();
    append(null, buffer, strategy);
    return buffer.toString();
}

因此,maven 构建在编译时失败,示例错误如下:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project test: Compilation failure
[ERROR] /D:/test/jaxb2/target/generated-sources/xjc/xsd/file/File.java:[73,64] incompatible types: org.jvnet.jaxb2_commons.lang.ToStringStrategy cannot be converted to org.jvnet.jaxb2_commons.lang.ToStringStrategy2

出于某种原因,当我使用与 org.jvnet.jax-ws-commons.jaxws-maven-plugin 相同的配置从 WSDL 生成 classes 时,使用了正确的 INSTANCE2 .我还尝试通过以下方式明确给出要使用的 class:

-XtoString-toStringBuilder=org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy

-XtoString-toStringStrategyClass=org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy

但没有任何改变。有人有类似的问题吗?下面的示例:

pom.xml

<dependencies>
    <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-basics-runtime</artifactId>
        <version>0.12.0</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.14.0</version>
            <executions>
                <execution>
                    <id>generate-raml</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>./</schemaDirectory>
                        <extension>true</extension>
                        <args>
                            <arg>-XtoString</arg>
                        </args>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.12.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

XSD [应该放在pom旁边]:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:file="file.xsd"
  targetNamespace="file.xsd"
    elementFormDefault="qualified">
  <xsd:element name="file">
    <xsd:complexType>
      <xsd:attribute name="id" type="xsd:string" use="optional"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

尝试为 jaxb2-basics 和 jaxb2-basics-runtime 使用版本“1.11.1”而不是“0.12.0”