mavenxsdxjcmaven-jaxb2-pluginxjb

Maven-Jaxb2 Plugin Type references Are throwing Already Defined Errors


I'm new with Jaxb plugin to generate models. I have 2 xsd file , where one xsd is referencing type in other xsd

common.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema attributeFormDefault="unqualified" 
    elementFormDefault="qualified" 
    targetNamespace="http://www.example.com/schemas/common" 
    version="1.0" xmlns="http://www.example.com/schemas/common" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

        <xsd:complexType name="person">
        <xsd:sequence>
            <xsd:element minOccurs="1" name="gender" type="xsd:string"/>
            <xsd:element minOccurs="1" name="dateOfBirth" type="xsd:string"/>
            <xsd:element minOccurs="1" name="name" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

pupil.xsd , this has refernce to person defined in common.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema attributeFormDefault="unqualified" 
        elementFormDefault="qualified" 
        targetNamespace="http://www.example.com/schemas/pupil" version="1.0"
        xmlns:common="http://www.example.com/schemas/common"
        xmlns="http://www.example.com/schemas/pupil" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

        <xsd:import namespace="http://www.example.com/schemas/common" schemaLocation="http://www.example.com/domain/schemas/common/common.xsd"/>

        <xsd:complexType name="student">
        <xsd:sequence>
                <xsd:element maxOccurs="1" minOccurs="1" name="class" type="xsd:string"/>
                <xsd:element maxOccurs="1" minOccurs="1" name="generralInfo" type="common:person"/>             
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>       

catalog.cat , try to use episode feature of plugin

REWRITE_SYSTEM "http://www.example.com/domain/schemas/common/common.xsd" "."

pom.xml, this my maven build configuration

<build>
    <defaultGoal>test</defaultGoal>
    <plugins>
      <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <configuration>
          <extension>true</extension>
          <catalog>src/main/resources/catalog.cat</catalog>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

while i run mvn clean install, I'm getting following Error

[ERROR] Error while parsing schema(s).Location [ http://www.example.com/domain/schemas/common/common.xsd{XXX,XX}]. org.xml.sax.SAXParseException; systemId: http://www.example.com/domain/schemas/common/common.xsd; lineNumber: XXX; columnNumber: XXX; 'person' is already defined

I tried to resolve this errors using various option mentioned here, but it's help me. I think I'm doing something wrong in the binding configuration or pom configuration

Could some one point out what is wrong here


Solution

  • I found out solution for my problem, there were so many thing i was doing wrong

    1) Change

    <xsd:include>  to <xsd:import>
    

    2) Keep the name space same for common.xsd and pupil.xsd

    3) After these changes I followed , the steps from this answer

    Here is sample project from the author of plugin itself ,that address the same issue i was facing