I’m using XJC to generate Java classes from the HTNG Payment Systems schemas, available in the /schemas directory of this zip.
If I run XJC without passing the -p parameter, generation works fine, and classes are generated under the org.htng._2009b package:
$ cd schemas
$ xjc -mark-generated -no-header -target 2.1 -npa .
All I want to do is change the package name of the generated classes to something more project appropriate, e.g. com.justin.htng:
$ cd schemas
$ xjc -mark-generated -no-header -target 2.1 -npa -p com.justin.htng .
However, doing this breaks JAXB generation spectacularly, throwing collision errors on almost every element. I can work around this by generating the classes via the first method, and then refactoring them to the com.justin.htng package, but that’s not very maintainable and doesn’t port well to the maven-jaxb2-plugin.
Can someone explain why this is happening, and if there is a way to work around it with XJC? I feel like if JAXB works via the first method, it should work via the second, since all that needs to be done is a simple String substitution for the package name. I suppose I could use the second method and handle all of the collisions via an .xjb bindings file, but that would be tedious given the number of errors.
When I run
I get two sets of generated classes, one in the package named
generatedand the other inorg.htng._2009b. TheHTNG_CommonTypes.xsdschema doesn’t have atargetNamespace, so when it is compiled directly its types will end up in thegeneratedpackage. ButHTNG_CommonTypesis also imported by the other schemas, which do declare atargetNamespace. When the common types are compiled in the imported case, they end up inorg.htng._2009b.Now if you specify the
-poption toxjc, that overrides the namespace-to-package mapping for all namespaces, so both the namespaced and non-namespaced types get mapped to the same package, hence the name clashes.I suspect the correct solution here is simply not to compile the
CommonTypesschema independently, but only to compile the ones that import it. The following works for me: