While generating binding classes with JAXB I’ve recently encountered the following problem:
parsing a schema...
compiling a schema...
[ERROR] A class/interface with the same name "net.solidsyntax.examples.jaxb.oxm.AnXSDType"is already inuse. Use a class customization to resolve this conflict.
[ERROR] (Relevant to above error) another "AnXSDType"is generated from here.
[ERROR] Two declarations cause a collision in the ObjectFactory class.
[ERROR] (Related to above error) This is the other declaration.
Failed to produce code.
The problem occurred while trying to generate binding classes from an xsd (MySchema.xsd) which included CommonTypes.xsd and EvenMoreCommonTypes.xsd. Both CommonTypes.xsd and EvenMoreCommonTypes.xsd contain a type called “AnXSDType”. By default JAXB generates binding files with the same name as the type. Two source files with the same name in the same package is not possible so JAXB throws generates an exception.
How can we fix this ?
We can use JAXB class customization to specify a custom name for the binding. In this case we want the “AnXSDType” from CommonTypes.xsd to generate a binding file named “AnXSDTypeFromCommonTypes” and the one from EvenMoreCommonTypes.xsd should generate a file called “AnXSDTypeFromEvenMoreCommonTypes”. In order to do so we need to create a custom bindings file (here called binding.xml)