获取错误在使用maven-jaxb2-plugin

时间:2016-04-21 15:07:20

标签: jaxb2 maven-jaxb2-plugin xjb

我正在使用maven-jaxb2-plugin。这是我的插件配置

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <executions>
        <execution>
            <id>xjc-serviceoperations</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <generateDirectory>${basedir}/src/main/java/</generateDirectory>
                <schemaDirectory>src/main/resources/schemas/lmsapi/serviceoperations</schemaDirectory>
                <removeOldOutput>false</removeOldOutput>
            </configuration>
        </execution>
        <execution>
            <id>xjc-types</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <generateDirectory>${basedir}/src/main/java</generateDirectory>
                <schemaDirectory>src/main/resources/schemas/lmsapi/types</schemaDirectory>
                <bindingDirectory>src/main/resources/schemas</bindingDirectory>
                <bindingIncludes>
                    <include>schema-binding.xjb</include>
                </bindingIncludes>
                <removeOldOutput>false</removeOldOutput>
            </configuration>
        </execution>
    </executions>
</plugin>   

这是我的schema-binding.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb 
                    http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    version="2.1">

    <jaxb:globalBindings>
        <jaxb:javaType name="java.time.LocalDateTime" xmlType="xsd:dateTime"
            parseMethod="com.softech.ls360.lms.api.proxy.xsd.binder.XSDateTimeCustomBinder.parseDateTime"
            printMethod="com.softech.ls360.lms.api.proxy.xsd.binder.XSDateTimeCustomBinder.printDateTime" />

    <jaxb:javaType name="java.time.LocalDate" xmlType="xsd:date"
            parseMethod="com.softech.ls360.lms.api.proxy.xsd.binder.XSDateCustomBinder.parseDateTime"
            printMethod="com.softech.ls360.lms.api.proxy.xsd.binder.XSDateCustomBinder.printDateTime" />

        <xjc:serializable uid="1" /> 
    </jaxb:globalBindings>

</jaxb:bindings>

我有一个xsd Enrollments.xsd。其中我有日期类型。我希望任何带有日期或日期时间类型的xsd都转换为Localdate或Localdatetime而不是XMLGregorianCalendar。这是片段

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://enrollment.types.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns="http://enrollment.types.lmsapi.message.webservice.lms.vu360.softech.com" 
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

    <xsd:include schemaLocation="Learner.xsd"/>
    ...
    <xsd:complexType name="LearnerEnrollCourses">  
        <xsd:sequence>  
            <xsd:element name="CourseId" type="xsd:string" minOccurs="1" maxOccurs="unbounded" nillable="false"  />
        </xsd:sequence> 
        <xsd:attribute name="enrollmentStartDate" type="xsd:date" use="required" />
         <xsd:attribute name="enrollmentEndDate" type="xsd:date" use="required" /> 
    </xsd:complexType>
    ...
</xsd:schema>

现在我右键点击POM。选择Run As -> Maven generate-sources然后我收到以下错误

[INFO] Sources are not up-to-date, XJC will be executed.
[ERROR] Error while parsing schema(s).Location [ file:/D:/Basit/eclipse-jee-mars-2/workspace/360Training/Java%208/LS360ProxyAPI/LmsApiProxy/src/main/resources/schemas/schema-binding.xjb{12,26}].
com.sun.istack.SAXParseException2; systemId: file:/D:/Basit/eclipse-jee-mars-2/workspace/360Training/Java%208/LS360ProxyAPI/LmsApiProxy/src/main/resources/schemas/schema-binding.xjb; lineNumber: 12; columnNumber: 26; only one globalBindings customization is allowed in a whole compilation
at com.sun.tools.xjc.ErrorReceiver.error(ErrorReceiver.java:86)
at com.sun.tools.xjc.reader.xmlschema.ErrorReporter.error(ErrorReporter.java:84)
....
[ERROR] Error while parsing schema(s).Location [ file:/D:/Basit/eclipse-jee-mars-2/workspace/360Training/Java%208/LS360ProxyAPI/LmsApiProxy/src/main/resources/schemas/schema-binding.xjb{12,26}].
com.sun.istack.SAXParseException2; systemId: file:/D:/Basit/eclipse-jee-mars-2/workspace/360Training/Java%208/LS360ProxyAPI/LmsApiProxy/src/main/resources/schemas/schema-binding.xjb; lineNumber: 12; columnNumber: 26; (related to above) but one is already given at this location

虽然正在产生类。但是类型仍然是XMLGregorianCalendar。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LearnerEnrollCourses", propOrder = {
    "courseId"
})
public class LearnerEnrollCourses {

    @XmlElement(name = "CourseId", required = true)
    protected List<String> courseId;

    @XmlAttribute(name = "enrollmentStartDate", required = true)
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar enrollmentStartDate;

    @XmlAttribute(name = "enrollmentEndDate", required = true)
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar enrollmentEndDate;

我在配置方面做错了什么,如何解决?

由于

--------------------- EDIT ---------------------- -

此插件配置正常运行。

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <executions>
        <execution>
            <id>xjc-serviceoperations</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <generateDirectory>${basedir}/src/main/java/</generateDirectory>
                <schemaIncludes>
                    <schemaInclude>schemas/lmsapi/serviceoperations/*.xsd</schemaInclude>
                </schemaIncludes>
                <bindingIncludes>
                    <bindingInclude>schemas/schema-binding.xjb</bindingInclude>
                </bindingIncludes>
                <verbose>true</verbose>
                <extension>true</extension>
                <removeOldOutput>false</removeOldOutput>
            </configuration>
        </execution>
        <execution>
            <id>xjc-types</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <generateDirectory>${basedir}/src/main/java</generateDirectory>
                <schemaDirectory>schemas/lmsapi/types</schemaDirectory>
                <schemaIncludes>
                    <schemaInclude>**/*.xsd</schemaInclude>
                </schemaIncludes>
                <schemaExcludes>
                    <schemaExclude>Enrollment.xsd</schemaExclude>
                </schemaExcludes>
                <removeOldOutput>false</removeOldOutput>
                <verbose>true</verbose>
                <extension>true</extension>
            </configuration>
        </execution>
    </executions>
</plugin>

这是我的schema-binding.xjb

<jaxb:globalBindings>

    <jaxb:javaType name="java.time.LocalDateTime" xmlType="xsd:dateTime"
            parseMethod="com.softech.ls360.lms.api.proxy.schema.binder.XSDateTimeCustomBinder.parseDateTime"
            printMethod="com.softech.ls360.lms.api.proxy.schema.binder.XSDateTimeCustomBinder.printDateTime" />

    <jaxb:javaType name="java.time.LocalDate" xmlType="xsd:date"
            parseMethod="com.softech.ls360.lms.api.proxy.schema.binder.XSDateCustomBinder.parseDateTime"
            printMethod="com.softech.ls360.lms.api.proxy.schema.binder.XSDateCustomBinder.printDateTime" />

    <!-- Force all classes implements Serializable -->
    <xjc:serializable uid="1" />

</jaxb:globalBindings>


<jaxb:bindings schemaLocation="lmsapi/types/Enrollment.xsd" node="/xsd:schema" >
    <jaxb:schemaBindings >
        <jaxb:package name="com.softech.vu360.lms.webservice.message.lmsapi.types.enrollment" />
    </jaxb:schemaBindings>
</jaxb:bindings>

这是我的serviceoperations和types目录中的一个文件

EnrollmentServiceOperations.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://enrollment.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns="http://enrollment.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns:enrolmnt="http://enrollment.types.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns:tr="http://transactionresult.types.lmsapi.message.webservice.lms.vu360.softech.com"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xsd:import namespace="http://transactionresult.types.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="../types/TransactionResultType.xsd"/>
    <xsd:import namespace="http://enrollment.types.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="../types/Enrollment.xsd"/>

    <xsd:element name="LearnerCoursesEnrollRequest">
        <xsd:complexType>
            ....
        </xsd:complexType>
    </xsd:element>
    ....
</xsd:schema>

CustomerServiceOperations.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://customer.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns="http://customer.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns:cust="http://customer.types.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns:tr="http://transactionresult.types.lmsapi.message.webservice.lms.vu360.softech.com"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xsd:import namespace="http://transactionresult.types.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="../types/TransactionResultType.xsd"/>
    <xsd:import namespace="http://customer.types.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="../types/Customer.xsd"/>

    <xsd:element name="AddCustomerRequest">
        <xsd:complexType>
            ...
        </xsd:complexType>
    </xsd:element>
    ...
</xsd:schema>

类型/ Enrollment.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://enrollment.types.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns="http://enrollment.types.lmsapi.message.webservice.lms.vu360.softech.com" 
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

    <xsd:include schemaLocation="Learner.xsd"/>

    <xsd:complexType name="LearnerCourses">  
        ....  
    </xsd:complexType>
    ....
</xsd:schema>

类型/ Customer.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://customer.types.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns="http://customer.types.lmsapi.message.webservice.lms.vu360.softech.com" 
    xmlns:addr="http://address.types.lmsapi.message.webservice.lms.vu360.softech.com"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

    <xsd:import namespace="http://address.types.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="Address.xsd"/>

    <xsd:complexType name="Customers">  
        <xsd:sequence>  
            ....
        </xsd:sequence>  
    </xsd:complexType>
    ...
</xsd:schema>

现在我运行Run As -> Maven generate-sources。它将在src/main/java/com/..../serviceoperation/customer/AddCustomerRequest.java中生成服务操作源,并在src/main/java/com/.../types/customer/Customers.java中键入源。与其他人相似xsds。

为什么我不能使用<generateDirectory>${basedir}/src/main/java/</generateDirectory>。这就是我要的。我认为这个插件是从每个xsd中的targetnamespace创建包。我是对的吗?

如果我在配置此插件时出错了,请告诉我,我可以更正它。就像你说的那样,不要在src / main / java中生成。

我希望serviceoperations文件夹(src/main/resources/schemas/lmsapi/serviceoperation/*.xsd)中的所有xsds都应在src/main/java/com/..../serviceoperation/customer/*.javasrc/main/java/com/..../serviceoperation/enrollment/*.java文件夹中生成,而其他文件夹则相同。

(src/main/resources/schemas/lmsapi/types/*.xsd)中的所有xsds应在src/main/java/com/..../types/customer/*.javasrc/main/java/com/..../types/enrollment/*.java文件夹中生成,其他文件夹也相同。

此外,我的绑定文件也适用于servieoperations和types文件夹中的所有xsds (src/main/resources/schemas/schema-binding.xjb)所有xsds。

有没有更好的方法来配置这个插件然后请告诉我所以我可以纠正它在我身边。我猜你是这个插件的作者。

谢谢&amp;此致

Basit Mahmood Ahmed

2 个答案:

答案 0 :(得分:3)

该问题的当前状态,以免其他人感到沮丧和搜索...

此问题随机出现,是由JAXB实现中的错误引起的。该问题已报告https://java.net/jira/browse/JAXB-687但是现在已经关闭,即使它没有修复(请参阅2016年9月16日发布的次优用户的评论)。

答案 1 :(得分:2)

遇到类似的问题并通过更改java version from 1.8 -> 1.7解决了这个问题。

或者删除xjc:serializable uid="1"也可以。