Search code examples
javaxsdequalshashcodecxf-xjc-plugin

How do I generate equals and hashCode with the CXF xjcplugin


I am trying to generate a Java class containing the methods toString, equals and hashCode from an XSD file. I got toString working, but I am not able to figure out how to get the CXF plugin to generate the equals and the hashCode methods.

This is my XSD file :

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/messagedefinitions"
           xmlns="http://www.example.com/messagedefinitions">


    <xs:element name="Message">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="status" type="Status"/>
                <xs:element name="id" type="Identifier"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="Status">
        <xs:restriction base="xs:string">
            <xs:maxLength value="4"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="Identifier">
        <xs:restriction base="xs:int"/>
    </xs:simpleType>

</xs:schema>

My working POM (what does not work is commented out) :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>xsd-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>xsd-demo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cxf.version>2.6.1</cxf.version>
        <cxf.xjc.plugin.version>2.6.1</cxf.xjc.plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-tools-common</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf.xjc-utils</groupId>
            <artifactId>xjc-utils</artifactId>
            <type>pom</type>
            <version>${cxf.xjc.plugin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf.xjc-utils</groupId>
            <artifactId>cxf-xjc-runtime</artifactId>
            <version>${cxf.xjc.plugin.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-xjc-plugin</artifactId>
                <version>${cxf.xjc.plugin.version}</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xsdtojava</goal>
                        </goals>

                        <configuration>
                            <sourceRoot>${basedir}/target/generated-sources</sourceRoot>
                            <xsdOptions>
                                <xsdOption>
                                    <extension>true</extension>
                                    <xsd>${basedir}/src/main/resources/xsd/Message.xsd</xsd>
                                    <bindingFile>${basedir}/src/main/resources/bindings.xml</bindingFile>
                                    <extensionArgs>
                                        <arg>-Xts:style:multiline</arg>
                                        <!--<arg>-Xequals</arg>-->
                                        <!--<arg>-XhashCode</arg>-->
                                    </extensionArgs>
                                </xsdOption>
                            </xsdOptions>
                            <extensions>
                                <extension>org.apache.cxf.xjcplugins:cxf-xjc-ts:${cxf.xjc.plugin.version}</extension>
                                <!--<extension>org.apache.cxf.xjcplugins:cxf-xjc-XhashCode:${cxf.xjc.plugin.version}-->
                                <!--</extension>-->
                                <!--<extension>org.apache.cxf.xjcplugins:cxf-xjc-Xequals:${cxf.xjc.plugin.version}-->
                                <!--</extension>-->
                            </extensions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

The bindings file :

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        jaxb:version="2.1">
    <jaxb:globalBindings>
        <xjc:serializable uid="1234"/>
    </jaxb:globalBindings>
</jaxb:bindings>

If I un-comment the commented-out lines for the Xequals I get this error :

[ERROR] Failed to execute goal org.apache.cxf:cxf-xjc-plugin:2.6.1:xsdtojava (generate-sources) on project xsd-demo: Could not download extension artifact: Requested download does not exist. Could not find artifact org.apache.cxf.xjcplugins:cxf-xjc-Xequals:jar:2.6.1 in central (http://repo1.maven.org/maven2)

Fine, I realise that the artifact does not exist, but I do not know then how to configure the CXF plugin to generate the equals and hashCode methods. Do you know how I can configure the CXF plugin to generate the equals and hashCode methods ? Thanks in advance for any help.


Solution

  • I was not able to get the cxf-xjc-plugin to generate the equals and hashCode methods from the XML schema files. I gave up on the cxf-xjc-plugin and turned to the maven-jaxb2-plugin instead. This is the plugin configuration I used :

    <plugin>
      <groupId>org.jvnet.jaxb2.maven2</groupId>
      <artifactId>maven-jaxb2-plugin</artifactId>
      <version>0.8.3</version>
      <executions>
        <execution>
          <id>generate</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <extension>true</extension>
              <generatePackage>my.package.name</generatePackage>
              <args>
                <arg>-XtoString</arg>
                <arg>-Xequals</arg>
                <arg>-XhashCode</arg>
              </args>
              <schemaDirectory>${basedir}/src/main/resources</schemaDirectory>
              <schemaIncludes>
                <include>xsd/*.xsd</include>
              </schemaIncludes>
              <plugins>
                <plugin>
                  <groupId>org.jvnet.jaxb2_commons</groupId>
                  <artifactId>jaxb2-basics</artifactId>
                  <version>0.6.4</version>
                </plugin>
              </plugins>
            </configuration>
          </execution>
      </executions>
    </plugin>