0

I am getting below error while maven build using jaxb(maven-jaxb2-plugin) compiler

A class/interface with the same name "org.somePackage.customer" is already in use.
    Use a class customization to resolve this conflict.
        at com.sun.tools.xjc.util.CodeModelClassFactory.createClass(CodeModelClassFactory.java:121)
        at com.sun.tools.xjc.util.CodeModelClassFactory.createClass(CodeModelClassFactory.java:82)
        at com.sun.tools.xjc.generator.bean.ImplStructureStrategy$1.createClasses(ImplStructureStrategy.java:82)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.generateClassDef(BeanGenerator.java:437)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.getClazz(BeanGenerator.java:469)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.<init>(BeanGenerator.java:194)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.generate(BeanGenerator.java:166)
        at com.sun.tools.xjc.model.Model.generateCode(Model.java:290)
        at org.jvnet.mjiip.v_2_2.XJC22Mojo.generateCode(XJC22Mojo.java:70)

Relevant XSD code snippet because of which error is coming

 <xsd:element name="customer" >
         ........
 </xsd:element>
 <xsd:element name="permanentCustomer" type="customer"/>

Relevant pom section for maven-jaxb2-plugin is

    <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.3</version>
            <configuration>
                <schemaDirectory>src/main/resources/META-INF/schema</schemaDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

What works

If I introduce block="substitution" it works

<xsd:element name="customer" block="substitution">
 ........
</xsd:element>
<xsd:element name="permanentCustomer" type="customer"/>

After doing research on net,My guess is jaxb compiler again trying to create the customer class based on element name="permanentCustomer" which it has already created based on element name ="customer". So I don't want to create java object for permanentCustomer while unmarshalling as it has been already created while customer creation.

I can think of two solutions

1)Some configuration For maven-jaxb2-plugin plugin where if java class has been already generated, don't to regenerate it again and proceed

2)Or Mentioning some attribute at xml level to ignore specific elements ?

Is there any configuration exists ?

emilly
  • 10,060
  • 33
  • 97
  • 172

1 Answers1

0

This is a question, not specific to which I (diclaimer) happen to be an author of.

Ignoring the generation of the Customer class for the customer element since it was already generated for the customer type is not a good idea as these are different concepts. Don't cure the symptom.

The core problem is that XJC tries tries to create a class for the customer element and this causes a naming collision. So either don't generate the class fo the customer element or do something about the naming collission.

I'm not sure why XJC tries to create a class for the customer element. Normally it should not be the case. You might have some config jumping in for this, not quite sure.

Adressing the naming collision is quite easy. You could probably do something like:

<jaxb:bindings 
    schemaLocation="..." 
    node="/xs:schema">

    <jaxb:bindings node="xs:element[@name='customer']">
        <jaxb:class name="CustomerElement"/>
    </jaxb:bindings>
</jaxb:bindings>

Alternatively you can use a global customization for name transforms, see this answer:

https://stackoverflow.com/a/21685303/303810

Something like:

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • His customer element likely includes an anonymous type definition within it. Since it has no name it uses the element name instead which collides with the actual customer type defined elsewhere. – tdrury Jul 09 '15 at 21:39
  • @tdrury Sounds plausible. – lexicore Jul 09 '15 at 21:48