While generating binding classes with JAXB I’ve recently encountered the following problem:
1
2
3
4
5
6
7
8
9
10
11
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.
line15offile:/C:/xsd/CommonTypes.xsd
[ERROR] (Relevant to above error) another "AnXSDType"is generated from here.
line10offile:/C:/xsd/EvenMoreCommonTypes.xsd
[ERROR] Two declarations cause a collision in the ObjectFactory class.
line15offile:/C:/xsd/CommonTypes.xsd
[ERROR] (Related to above error) This is the other declaration.
line10offile:/C:/xsd/EvenMoreCommonTypes.xsd
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)