我有一个由jaxb2-maven-plugin
使用XML schema
生成的Task类。
这是模式:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
targetNamespace="http://www.w3schools.com"
jaxb:version="2.1">
<xs:element name="experiment">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
<xs:element name="globalVars">
<xs:complexType>
<xs:sequence>
<xs:element name="var" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="tasks">
<xs:complexType>
<xs:sequence>
<xs:element name="task" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="command"/>
<xs:element name="dependencies" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="dependency" maxOccurs="unbounded"
minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="requirements" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="ram"/>
<xs:element type="xs:byte" name="cores"/>
<xs:element type="xs:string" name="storage"/>
<xs:element type="xs:string" name="timeout"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
以及生成的Task
类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"id",
"command",
"dependencies",
"requirements"
})
@Generated(value = "com.sun.tools.xjc.Driver", date = "2018-11-03T03:54:10-03:00", comments = "JAXB RI v2.3.0")
public class Task {
@XmlElement(required = true)
@Generated(value = "com.sun.tools.xjc.Driver", date = "2018-11-03T03:54:10-03:00", comments = "JAXB RI v2.3.0")
protected String id;
@XmlElement(required = true)
@Generated(value = "com.sun.tools.xjc.Driver", date = "2018-11-03T03:54:10-03:00", comments = "JAXB RI v2.3.0")
protected String command;
@Generated(value = "com.sun.tools.xjc.Driver", date = "2018-11-03T03:54:10-03:00", comments = "JAXB RI v2.3.0")
@XmlElementWrapper
@XmlElement(name = "dependency", namespace = "http://www.w3schools.com")
protected List<String> dependencies = new ArrayList<String>();
@Generated(value = "com.sun.tools.xjc.Driver", date = "2018-11-03T03:54:10-03:00", comments = "JAXB RI v2.3.0")
protected Requirements requirements;
// Some getters & setters
}
我想向该Task
类添加一个方法,所以我搜索了一个解决方案并找到了这篇帖子Adding extra methods to a JAXB class generated from schema。
因此,我创建了一个TaskNode
类作为子类,并为XML绑定文件添加了自定义项:
TaskNode
:
public class TaskNode extends Task {
public void doSomething() {
// ...
}
}
XML绑定:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<!-- The customization -->
<jaxb:bindings schemaLocation="../schema/experiment.xsd" node="/descendant::xs:element[@name='task']">
<jaxb:class implClass="br.ufop.decom.TaskNode"/>
</jaxb:bindings>
<!-- Generate a class for all xml element instead a single class for everything -->
<jaxb:globalBindings localScoping="toplevel"/>
</jaxb:bindings>
但是此解决方案给了我这个错误:
[ERROR] file:/D:/IntelliJ%20Projects/Experimentor/src/main/resources/bindings/experiment.xjb [7,53]
org.xml.sax.SAXParseException; systemId: file:/D:/IntelliJ%20Projects/Experimentor/src/main/resources/bindings/experiment.xjb; lineNumber: 7; columnNumber: 53; A class/interface with the same name "br.ufop.decom.Task" is already in use. Use a class customization to resolve this conflict.
如何解决此错误?
这是我的项目设置pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.ufop.decom</groupId>
<artifactId>experimentor</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<locale>us</locale>
<sources>
<source>src/main/resources/schema/experiment.xsd</source>
</sources>
<xjbSources>
<xjbSource>src/main/resources/bindings/experiment.xjb</xjbSource>
</xjbSources>
<packageName>br.ufop.decom</packageName>
<readOnly>true</readOnly>
<verbose>true</verbose>
<addGeneratedAnnotation>true</addGeneratedAnnotation>
<encoding>UTF-8</encoding>
<extension>true</extension>
<arguments>
<argument>-Xxew</argument>
<argument>-Xfluent-api</argument>
<argument>-Xjaxbindex</argument>
</arguments>
</configuration>
<dependencies>
<dependency>
<groupId>com.github.jaxb-xew-plugin</groupId>
<artifactId>jaxb-xew-plugin</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>net.java.dev.jaxb2-commons</groupId>
<artifactId>jaxb-fluent-api</artifactId>
<version>2.1.8</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.12.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>