问题
I am using maven-jaxb2-plugin. Here is my plugin configuration
<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>
here is my schema-binding.xml file
<?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>
I have an xsd Enrollments.xsd
. In which I have date type. I want that any xsd with date or datetime type convert to Localdate or Localdatetime instead of XMLGregorianCalendar. here is the snippet
<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>
Now when I right click on POM. select Run As -> Maven generate-sources
then I am getting the following error
[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
Although classes are generating. But type is still 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;
What I am doing wrong in configuration and how can I solve it ?
Thanks
---------------------EDIT-----------------------
This plugin configuration is working.
<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>
here is my 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>
Here is my one of the files from serviceoperations and types directory
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>
Types/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>
types/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>
Now when I run Run As -> Maven generate-sources
. It will generate serviceoperations sources in src/main/java/com/..../serviceoperation/customer/AddCustomerRequest.java
and types sources in src/main/java/com/.../types/customer/Customers.java
. Similar for others xsds.
Why shouldn't I use <generateDirectory>${basedir}/src/main/java/</generateDirectory>
. This is what I want. I think this plugin is making packages from targetnamespace in each xsd. Am I right ?
If I am doing something wrong in configuring this plugin then please tell me so I can correct it. Like you said not to generate in src/main/java.
I want that all my xsds in serviceoperations folder (src/main/resources/schemas/lmsapi/serviceoperation/*.xsd)
should generate in src/main/java/com/..../serviceoperation/customer/*.java
, src/main/java/com/..../serviceoperation/enrollment/*.java
folder and same for others.
All xsds in (src/main/resources/schemas/lmsapi/types/*.xsd)
should generate in src/main/java/com/..../types/customer/*.java
, src/main/java/com/..../types/enrollment/*.java
folder and same for others.
And also my binding file apply to all xsds (src/main/resources/schemas/schema-binding.xjb)
all xsds in servieoperations and types folder.
Is there any better way to configure this plugin then please tell me so I can correct it on my side. I am guessing you are the author of this plugin.
Thanks & Regards
Basit Mahmood Ahmed
回答1:
Current status of that issue, to spare others frustration and searching...
This issue shows up randomly and is caused by a bug in JAXB implementation. The issue was reported https://java.net/jira/browse/JAXB-687 however it is closed now, even though it is not fixed (see comment from suboptimal user posted on 16 Sep 2016).
回答2:
Had a similar problem and solved it by changing the java version from 1.8 -> 1.7
.
Alternatively removing xjc:serializable uid="1"
worked, too.
来源:https://stackoverflow.com/questions/36773646/getting-error-only-one-globalbindings-customization-is-allowed-in-a-whole-compil