JAXB 2.1为生成的类实现Comparable <t>

时间:2018-05-10 14:41:18

标签: java jaxb jaxb2 jaxb2-maven-plugin jaxb2-simplify-plugin

  • 使用Jaxb 2.1从.xsd
  • 生成java代码
  • 使用Jaxb2-basics插件
  • 想要生成Class Student来实现Comparable。

    public class Student implements Serializable, Comparable<Students> { ... //bean class... public int compareTo(Student other) { ..... return somevalue; } }

XSD:

<?xml version="1.0" encoding="UTF-8"?><jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance" 
jxb:version="2.1">
<jxb:bindings>
    <jxb:globalBindings>
        <jxb:javaType name="org.joda.time.LocalDate" xmlType="xs:date"
            parseMethod="Javax.xml.bind.DatatypeConverter.parseDateTime"
            printMethod="Javax.xml.bind.DatatypeConverter.parseDateTime" />
        <xjc:serializable uid="12343" />
    </jxb:globalBindings>
</jxb:bindings>
<jxb:bindings schemaLocation="Student.xsd"
    version="1.0" node="/xs:schema">
    <jxb:bindings node="//xs:element[@name='Student']/xs:complexType">
        <inheritance:implements>java.lang.Comparable&lt;Student&gt;</inheritance:imple‌​ments>
        <ci:code>public int compareTo(Student other) {

return somevalue;}</ci:code>
    </jxb:bindings>

插件:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>2.2</version>
        <executions>
            <execution>
                <id>xjc</id>
                <goals>
                    <goal>xjc</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <arguments>-extensions</arguments>
            <schemaDirectory>src/main/resources</schemaDirectory>
            <schemaincludes>
            <include>
            Student.xsd
            </include>
            </schemaincludes>
            <bindingDirectory>src/main/resources</bindingDirectory>
            <bindingincludes>
            <include>
            Student.xjb
            </include>
            </bindingincludes>
            <packageName>com.test.model.example</packageName>
            <outputDirectory>src/main/java </outputDirectory>

        </configuration>
    </plugin>

错误: 无法处理目录

中的模式文件

Unable to parse input schema(s). Error messages should have been provided. (org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.13.3:generate:default:generate-sources)org.apache.maven.plugin.MojoExecutionException: Unable to parse input schema(s). Error messages should have been provided. at org.jvnet.mjiip.v_2_2.XJC22Mojo.loadModel(XJC22Mojo.java:55) at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:40)

我错过了什么。任何有待解决的帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我不知道您在pom.xml中配置JAXB2基础知识的位置。

此处为guide。 TL; DR:

<project ...>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <extension>true</extension>
                    <args>
                        <arg>-Xinheritance</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>...</version>
                        </plugin>
                    </plugins>
                </configuration>
            </plugin>
        </plugins>
    </build>
    ...
</project>

您正在使用,我找不到在其文档中使用XJC插件的指南,但有this answer