简体   繁体   中英

JAXB 2.1 implement Comparable<T> for the generated Class

  • Using Jaxb 2.1 to generate java code from .xsd
  • Jaxb2-basics plug-in is used
  • Wants to have generated Class Student to implement 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:

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

Error: Could not process schema files in directory

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)

I am missing something. Any help to resolve is appreciated.

I don't see where you configure JAXB2 Basics in your pom.xml .

Here's a guide for . 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>

You're using , I could not find a guide for using XJC plugins in their documentation, but there's this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM