2

我正在尝试从 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 方法吗?提前感谢您的帮助。

4

3 回答 3

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>
于 2013-10-25T08:13:24.197 回答
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

于 2014-05-14T22:17:36.103 回答
0

对你来说肯定为时已晚……但是……

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

您需要 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 中将生成的目录添加到类路径我使用绑定文件(如有必要,请更改它)

    <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>
于 2015-08-22T12:03:33.583 回答