n1t4chi
n1t4chi

Reputation: 492

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

I've found an issue with toString generation when using maven-jaxb2-plugin with org.jvnet.jaxb2_commons.jaxb2-basics at version 0.12.0. Instead of generating JAXBToStringStrategy.INSTANCE2, it uses INSTANCE which is incompatible with ToStringStrategy2 as seen below:

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

Because of this, the maven build fails on compilation with example error below:

[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

For some reason, when I use same configuration with org.jvnet.jax-ws-commons.jaxws-maven-plugin to generate classes from WSDL, the correct INSTANCE2 is used. I also tried to explicitly give which class to use via:

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

or

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

but nothing changed. Anyone had similar issue? Example below:

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 [should be placed next to 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>

Upvotes: 2

Views: 2645

Answers (1)

Dmitry Kononov
Dmitry Kononov

Reputation: 77

try use version "1.11.1" instead of "0.12.0" for jaxb2-basics and jaxb2-basics-runtime

Upvotes: 3

Related Questions