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