如何使用CXF xjcplugin生成equals和hashCode

时间:2013-07-26 08:17:32

标签: java xsd equals hashcode cxf-xjc-plugin

我正在尝试从XSD文件生成包含toString,equals和hashCode方法的Java类。我得到了串工作,但我无法弄清楚如何让CXF插件生成equals和hashCode方法。

这是我的XSD文件:

<?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>

我的工作POM(什么不起作用被注释掉):

<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>

绑定文件:

<?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>

如果我取消注释Xequals的注释掉的行,我会收到此错误:

[错误]无法在项目xsd-demo上执行目标org.apache.cxf:cxf-xjc-plugin:2.6.1:xsdtojava(generate-sources):无法下载扩展工件:请求的下载不存在。找不到工件org.apache.cxf.xjcplugins:cxf-xjc-Xequals:jar:2.6.1 in central(http://repo1.maven.org/maven2

很好,我意识到工件不存在,但我不知道如何配置CXF插件来生成equals和hashCode方法。你知道我如何配置CXF插件来生成equals和hashCode方法吗?提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

我无法从cxf-xjc-plugin中获取XML模式文件中的equals和hashCode方法。我放弃了cxf-xjc-plugin,转而使用maven-jaxb2-plugin。这是我使用的插件配置:

<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>

答案 1 :(得分:0)

阅读此HOWTO之后,我就这样开始工作了:

<build>
   <plugins>
      <!-- Build WSDL resources (includes XSDs) -->
      <plugin>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-codegen-plugin</artifactId>
         <version>2.7.0</version>
         <executions>
            <execution>
               <id>generate-sources</id>
               <phase>generate-sources</phase>
               <goals>
                  <goal>wsdl2java</goal>
               </goals>
            </execution>
         </executions>
         <configuration>
            <defaultOptions>
               <extraargs>
                  <extraarg>-verbose</extraarg>
                  <extraarg>-xjc-Xequals</extraarg>
                  <extraarg>-xjc-XhashCode</extraarg>
               </extraargs>
               <bindingFiles>
                  <!-- Shared XSD file bindings -->
                  <bindingFile>${project.basedir}/src/main/resources/bindings.xjb</bindingFile>
               </bindingFiles>
            </defaultOptions>
            <wsdlOptions>
               <wsdlOption>
                  <wsdl>https://myserver.com/MyService?wsdl</wsdl>
                  <bindingFiles>
                     <!-- Specific WSDL file bindings -->
                     <bindingFile>${project.basedir}/src/main/resources/wsdl-bindings.xml</bindingFile>
                  </bindingFiles>
               </wsdlOption>
            </wsdlOptions>
         </configuration>
         <dependencies>
            <dependency>
               <groupId>org.jvnet.jaxb2_commons</groupId>
               <artifactId>jaxb2-basics</artifactId>
               <version>0.6.5</version>
            </dependency>
         </dependencies>
      </plugin>
   </plugins>
</build>

<dependencies>
   <!-- The generated equals and hashCode methods require some runtime libraries. -->
   <dependency>
      <groupId>org.jvnet.jaxb2_commons</groupId>
      <artifactId>jaxb2-basics-runtime</artifactId>
   </dependency>
</dependencies>

不幸的是,对我来说,使用jvnet jaxws-maven-plugin不是一个选项,因为我们的WSDL文件是由第三方提供给我们的,并且它们存在问题(例如soap:body内的wsdl:fault元素导致标准插件窒息的元素。

答案 2 :(得分:0)

你肯定来不及......但是......

我设法使用org.jvnet.jaxb2_commons生成哈希码,并使用插件cxf-xjc-plugin(如问题所示)等于:jaxb2-basics:0.7.0

你需要org.jvnet.jaxb2_commons:jaxb2-basics:0.7.0来生成方法(equals,hashcode)和org.jvnet.jaxb2_commons:jaxb2-basics-runtime:0.7.0用于编译依赖

使用antrun很难看但我需要它来为我的剧集生成一个目录 build-helper-maven-plugin的使用是将生成的目录添加到eclipse中的classpath 我使用绑定文件(必要时更改)

    <dependencies>
    <dependency>
        <groupId>org.apache.cxf.xjcplugins</groupId>
        <artifactId>cxf-xjc-ts</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf.xjc-utils</groupId>
        <artifactId>cxf-xjc-runtime</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-basics-runtime</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-basics</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>generate-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo message="Creating directories" />
                            <mkdir dir="./target/generated/src/main/resources/bindings/" />
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${basedir}/target/generated/src/main/java/</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>
                                    ${basedir}/target/generated/src/main/resources/bindings/</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-xjc-plugin</artifactId>
            <configuration>
                <extensions>
                    <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:3.0.3</extension>
                    <extension>org.apache.cxf.xjcplugins:cxf-xjc-ts:3.0.3</extension>
                    <extension>org.jvnet.jaxb2_commons:jaxb2-basics:0.7.0</extension>
                </extensions>
            </configuration>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>xsdtojava</goal>
                    </goals>
                    <configuration>
                        <xsdOptions>
                            <xsdOption>
                                <extension>true</extension>
                                <xsd>${basedir}/src/main/resources/schemas/FluxExportDenormalized.xsd</xsd>
                                <bindingFile>${basedir}/src/main/resources/bindings/binding.xjb</bindingFile>
                                <extensionArgs>
                                    <extensionArg>-Xdv</extensionArg>
                                    <extensionArg>-episode</extensionArg>
                                    <extensionArg>${basedir}/target/generated/src/main/resources/bindings/FluxExportTarifDenormalized.episode</extensionArg>
                                    <extensionArg>-Xts</extensionArg>
                                    <extensionArg>-Xequals</extensionArg>
                                    <extensionArg>-XhashCode</extensionArg>
                                </extensionArgs>
                            </xsdOption>
                            <xsdOption>
                                <extension>true</extension>
                                <xsd>${basedir}/src/main/resources/schemas/FluxExportNormalized.xsd</xsd>
                                <extensionArgs>
                                    <extensionArg>-Xdv</extensionArg>
                                    <extensionArg>-b</extensionArg>
                                    <extensionArg>${basedir}/src/main/resources/bindings/binding.xjb</extensionArg>
                                    <extensionArg>-b</extensionArg>
                                    <extensionArg>${basedir}/target/generated/src/main/resources/bindings/FluxExportDenormalized.episode</extensionArg>
                                    <extensionArg>-Xequals</extensionArg>
                                    <extensionArg>-XhashCode</extensionArg>
                                </extensionArgs>
                            </xsdOption>
                        </xsdOptions>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.7.0</version>
                </dependency>
            </dependencies>
        </plugin>

也许您需要在eclipse中使用生命周期以避免红叉错误:

<?xml version="1.0" encoding="UTF-8"?>
<lifecycleMappingMetadata>
    <pluginExecutions>
        <pluginExecution>
            <pluginExecutionFilter>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-xjc-plugin</artifactId>
                <versionRange>[2.3.0,)</versionRange>
                <goals>
                    <goal>xsdtojava</goal>
                </goals>
            </pluginExecutionFilter>
            <action>
                <execute />
            </action>
        </pluginExecution>
        <pluginExecution>
            <pluginExecutionFilter>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <versionRange>[1.4,)</versionRange>
                <goals>
                    <goal>run</goal>
                </goals>
            </pluginExecutionFilter>
            <action>
                <execute />
            </action>
        </pluginExecution>
    </pluginExecutions>
</lifecycleMappingMetadata>