如何使用 CXF xjcplugin 生成 equals 和 hashCode

问题描述 投票:0回答:4

我正在尝试从 XSD 文件生成一个包含 toString、equals 和 hashCode 方法的 Java 类。我得到了 toString 工作,但我无法弄清楚如何让 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 (http://repo1.maven.org/maven2)

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

java xsd equals hashcode cxf-xjc-plugin
4个回答
2
投票

我无法使用 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
投票

读完这篇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 文件是由第 3 方提供给我们的,并且它们存在问题(例如

soap:body
元素中的
wsdl:fault
元素),这会导致标准插件令人窒息。


1
投票

对你来说肯定太晚了...但是...

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

您需要 org.jvnet.jaxb2_commons:jaxb2-basics:0.7.0 来生成方法(等于、哈希码)和 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>

0
投票

download (zip) 是一个独立的 Maven 项目,用于演示来自 Apache CXF 的 Maven cxf-xjc-plugin 以及 XJC HiSrc BasicJAXB Plugins 的配置。具体来说,它配置了以下 XJC 插件:

  • -XhashCode - 生成无反射的 hashCode 方法。
  • -Xequals - 生成无反射的 equals 方法。
  • -XtoString - 生成无反射的 toString 方法。
  • -xfluentAPI - 为生成的代码启用 Fluent API、方法链接。

注意: 将下载解压到本地文件夹后,您可以使用以下命令运行测试和/或演示:

mvn -Ptest clean test
mvn -Pexec compile exec:java

商业模式

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

默认情况下,XJC 使用

Object
hashCodeequalstoString 方法为上述每个元素生成一个 JAXB 类。在此级别,对象通过对象 ID 进行比较。要按值比较/评估 JAXB 生成的
Message
对象,HiSrc BasicJAXB 插件
cxf-xjc-plugin
中配置为 <extensionArgs>

<extensionArgs>
    <extensionArg>-no-header</extensionArg>
    <extensionArg>-XhashCode</extensionArg>
    <extensionArg>-Xequals</extensionArg>
    <extensionArg>-XtoString</extensionArg>
    <extensionArg>-XfluentAPI</extensionArg>
    <extensionArg>-XfluentAPI-fluentMethodPrefix=use</extensionArg>
</extensionArgs>

对于单元测试,还激活 -XfluenceAPI 插件,以方便设置

Message
对象的测试实例。

FluentAPI测试

final Object object = getUnmarshaller().unmarshal(sample);
if ( object instanceof Message )
{
    Message msi1 = (Message) object;
    Message msi2 = OF.createMessage()
        .useStatus("PASS")
        .useId(1001);

    getLogger().debug("MSI1: {}", msi1);
    getLogger().debug("MSI2: {}", msi2);

    assertEquals(msi1, msi2, "Unmarshaled and Fluent MSIs are equal.");
}

JAR 冲突

上面的

cxf-xjc-plugin
明确声明了几个依赖项(未显示)来解决CXF产品和HiSrc产品之间的JAR冲突。由于这些产品是由独立团队生产的,因此可能存在一些版本冲突。 下载 (zip) 包含一个名为 ReadJarManifest.java 的工具,用于识别和解决冲突。冲突已在
download
示例中得到解决。

生成

build.log

mvn -Dorg.slf4j.simpleLogger.log.org.apache.cxf.maven_plugin.XSDToJavaMojo=DEBUG -Ptest clean test >build.log

ReadJarManifest.java

Read JAR Manifest from a JAR file or a LOG file.

Usage:
    javac ReadJarManifest.java
    java ReadJarManifest [jarfile | stdin]

Example:
    java ReadJarManifest <build.log

注意: ReadJarManifest.java 工具可以捕获大多数问题,但也可以报告 错误冲突。在此示例中,冲突百分比错误地高,因为两个不冲突的 jar 具有相同的版本号,但名称不同但相似。

Conflict%: 89
  file:/home/rick/.m2/repository/org/glassfish/jaxb/txw2/4.0.5/txw2-4.0.5.jar
  file:/home/rick/.m2/repository/org/glassfish/jaxb/xsom/4.0.5/xsom-4.0.5.jar

免责声明:我是 HiSrc 项目的维护者。

© www.soinside.com 2019 - 2024. All rights reserved.