在 0.12.0 版中使用 maven-jaxb2-plugin 和 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 生成类时,使用了正确的 INSTANCE2。我还尝试通过以下方式明确给出要使用的类:
-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>