Reputation: 8606
I have different .xsd files and I want to generate java classes from them in different packages. I configure maven maven-jaxb2-plugin
as following
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.11.0</version>
<executions>
<execution>
<id>generate-messages</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<forceRegenerate>true</forceRegenerate>
<generateDirectory>${basedir}/src/main/java</generateDirectory>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/resources/bindings</directory>
<includes>
<include>lmsApiBinding.xml</include>
</includes>
</fileset>
</schema>
</schemas>
</configuration>
</plugin>
Here is my file
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
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 generateElementProperty="false">
<xjc:serializable uid="1" />
</jaxb:globalBindings>
<jaxb:bindings schemaLocation="/src/main/webapp/schemas/lmsapi/serviceoperations/CustomerServiceOperations.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="abc.customer" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="/src/main/webapp/schemas/lmsapi/serviceoperations/EnrollmentServiceOperations.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="def.enrollment" />
</jaxb:schemaBindings>
</jaxb:bindings>
....
</jaxb:bindings>
Here is my CustomerServiceOperations.xsd
file
<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:sequence>
<xsd:element name="Customers" type="cust:Customers" minOccurs="1" maxOccurs="1" nillable="false" />
</xsd:sequence>
<xsd:attribute name="key" type="xsd:string" use="required" />
<xsd:attribute name="ResellerId" type="xsd:nonNegativeInteger" use="required" />
</xsd:complexType>
</xsd:element>
<xsd:element name="AddCustomerResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="RegisterCustomers" type="cust:RegisterCustomers" minOccurs="0" maxOccurs="1" nillable="false" />
</xsd:sequence>
<xsd:attribute name="transactionResult" type="tr:TransactionResultType" use="required"/>
<xsd:attribute name="transactionResultMessage" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
But when I click on my .pom
file and select Run As --> maven Generate Sources
Then I get the following errors
org.xml.sax.SAXParseException; systemId: file:/D:/Basit/eclipse-jee-luna/workspace/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml; lineNumber: 10; columnNumber: 19; s4s-elt-schema-ns: The namespace of element 'bindings' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.
...
org.xml.sax.SAXParseException; systemId: file:/D:/Basit/eclipse-jee-luna/workspace/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml; lineNumber: 10; columnNumber: 19; s4s-elt-invalid: Element 'bindings' is not a valid element in a schema document.
...
org.xml.sax.SAXParseException; systemId: file:/D:/Basit/eclipse-jee-luna/workspace/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml; lineNumber: 10; columnNumber: 19; schema_reference.4: Failed to read schema document 'file:/D:/Basit/eclipse-jee-luna/workspace/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
Why I am getting these errors ? What I am doing wrong?
Thanks
------Edit----
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/webapp/schemas/lmsapi</directory>
<includes>
<include>/serviceoperations/CustomerServiceOperations.xsd</include>
<include>/serviceoperations/EnrollmentServiceOperations.xsd</include>
<include>/serviceoperations/OrgGroupServiceOperations.xsd</include>
...
<include>/types/Address.xsd</include>
<include>/types/Course.xsd</include>
<include>/types/Customer.xsd</include>
....
<include>/types/UserGroup.xsd</include>
<include>/types/Vu360User.xsd</include>
</includes>
</fileset>
</schema>
</schemas>
--Final working code--
Hi,
Thanks. Finally got it working. Here is my file LmsApiServiceOperations.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://com/softech/vu360/lms/webservice/message/lmsapi/serviceoperations"
xmlns="http://com/softech/vu360/lms/webservice/message/lmsapi/serviceoperations"
elementFormDefault="qualified">
<xsd:import namespace="http://trainingplan.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="TrainingPlanServiceOperations.xsd"/>
<xsd:import namespace="http://customer.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="CustomerServiceOperations.xsd"/>
<xsd:import namespace="http://user.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="UserServiceOperations.xsd"/>
<xsd:import namespace="http://enrollment.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="EnrollmentServiceOperations.xsd"/>
<xsd:import namespace="http://securityroles.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="SecurityRoleServiceOperations.xsd"/>
<xsd:import namespace="http://orggroup.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="OrgGroupServiceOperations.xsd"/>
<xsd:import namespace="http://usergroup.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="UserGroupServiceOperations.xsd"/>
</xsd:schema>
Here is the plugin configuration
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.11.0</version>
<executions>
<execution>
<id>generate-messages</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<bindingDirectory>${basedir}/src/main/resources/bindings</bindingDirectory>
<bindingIncludes>
<bindingInclude>lmsApiBinding.xml</bindingInclude>
</bindingIncludes>
<generateDirectory>${basedir}/src/main/java/</generateDirectory>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/webapp/schemas/lmsapi/</directory>
<includes>
<include>serviceoperations/LmsApiServiceOperations.xsd</include>
</includes>
</fileset>
</schema>
</schemas>
<strict>true</strict>
<extension>true</extension>
<verbose>true</verbose>
<forceRegenerate>true</forceRegenerate>
</configuration>
</execution>
</executions>
</plugin>
Also my lmsApiBinding.xml file only contain now
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
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 generateElementProperty="false">
<xjc:serializable uid="1" />
</jaxb:globalBindings>
</jaxb:bindings>
Because if i use something like
<jaxb:globalBindings generateElementProperty="false">
<xjc:serializable uid="1" />
</jaxb:globalBindings>
<jaxb:bindings schemaLocation="CustomerServiceOperations.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.softech.vu360.lms.webservice.message.lmsapi.serviceoperations.customer" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="/schemas/lmsapi/serviceoperations/EnrollmentServiceOperations.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.softech.vu360.lms.webservice.message.lmsapi.serviceoperations.enrollment" />
</jaxb:schemaBindings>
</jaxb:bindings>
Then select Maven --> Generate sources. Then I get the following error
com.sun.istack.SAXParseException2; systemId: file:/D:/Basit/eclipse-jee-luna/workspace/360Training/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml; lineNumber: 17; columnNumber: 86; "file:/D:/Basit/eclipse-jee-luna/workspace/360Training/lms-api-1/src/main/resources/bindings/CustomerServiceOperations.xsd" is not a part of this compilation. Is this a mistake for "file:/D:/Basit/eclipse-jee-luna/workspace/360Training/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml"?
I was using binding file because I want to generate classes in different packages. But it seems it generate package name from targetnamespace. As I declared in my CustomerServiceOperations.xsd
file
<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:schema>
So after generating sources. It automatically put my CustomerServiceOperations.xsd
operations in package com.softech.vu360.lms.webservice.message.lmsapi.serviceoperations.customer
Thanks
Upvotes: 2
Views: 5316
Reputation: 8272
You put lmsApiBinding.xml
(bindings file) instead of your XSD CustomerServiceOperations.xsd
... I use this configuration in my project and works fine.
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.11.0</version>
<executions>
<execution>
<id>generate-messages</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<bindingDirectory>${basedir}/src/main/resources/bindings</bindingDirectory>
<bindingIncludes>
<bindingInclude>lmsApiBinding.xml</bindingInclude>
</bindingIncludes>
<generateDirectory>${basedir}/src/main/java/</generateDirectory>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/resources/schema/xsd</directory>
<includes>
<include>CustomerServiceOperations.xsd</include>
</includes>
</fileset>
</schema>
</schemas>
<strict>true</strict>
<extension>true</extension>
<verbose>true</verbose>
<forceRegenerate>true</forceRegenerate>
</configuration>
</execution>
</executions>
</plugin>
Upvotes: 1