Justin Garrick
Justin Garrick

Reputation: 14947

Why does changing the package name passed to XJC break JAXB generation?

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.

Upvotes: 2

Views: 2167

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

When I run

xjc -mark-generated -no-header -target 2.1 -npa .

I get two sets of generated classes, one in the package named generated and the other in org.htng._2009b. The HTNG_CommonTypes.xsd schema doesn't have a targetNamespace, so when it is compiled directly its types will end up in the generated package. But HTNG_CommonTypes is also imported by the other schemas, which do declare a targetNamespace. When the common types are compiled in the imported case, they end up in org.htng._2009b.

Now if you specify the -p option to xjc, 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 CommonTypes schema independently, but only to compile the ones that import it. The following works for me:

xjc -mark-generated -no-header -target 2.1 -npa -p com.justin.htng HTNG_P*.xsd

Upvotes: 3

Related Questions