I have a project which has a schema A and B, both within the same namespace. Both import schema C which also uses the same namespace. How can I generate JAXB classes for A a
Disclaimer: I am the author of maven-jaxb2-plugin.
TL;DR here's a test project which demonstrates how to do this.
This is possible, but is a bit hairy, so please bear with me.
If a.xsd, b.xsd and c.xsd are in the same namespace, a.xsd and b.xsd cannot import c.xsd, they can only include it. We want to generate each of the XSDs into its own package, say test.a, test.b and test.c and do it within the same single Maven project.
To do this we will need three separate executions of the maven-jaxb2-plugin, each configured with its own schema and target package. For example:
org.jvnet.jaxb2.maven2
maven-jaxb2-plugin
xjc-a
generate
test.a
${project.build.directory}/xjc-a
a.xsd
It is is important to use different target directories for separate executions.
OK, this would create three target directories with three target packages. Next problem is that classes from c.xsd will generated in test.a and test.b which we want to avoid.
To achieve this, we have to tell XJC to use classes from test.c for types from c.xsd. This is actually what episode file is for. This file is normally generated under META-INF\sun-jaxb.episode and it contains bindings for all types in the processed schema. Here's an example generated for c.xsd:
Episode file is actually a normal bindings file. So you can directly use it in compilation:
xjc-a
generate
test.a
${project.build.directory}/xjc-a
a.xsd
${project.build.directory}/xjc-c/META-INF
sun-jaxb.episode
There is just one tiny problem left. Episode files generated by XJC also contain this fragment:
It effectively says "do not generate code for schema in the given namespace". This would not be a problem if a.xsd or b.xsd would be in a different namespace. But since they are in the same namespace, this fragment will effectively turn off all code generation for a.xsd or b.xsd.
To work around this we can post-process the sun-jaxb.episode which was generated for c.xsd. This can be done with a simple XSLT:
This XSLT should be run after the code for c.xsd, but before the code for a.xsd and b.xsd is generated. This can be achieved by putting these executions into different phases (generate-sources, process-sources, generate-resources).
Below is the complete pom.xml:
4.0.0
org.jvnet.jaxb2.maven2
divide
0.0.1-SNAPSHOT
org.glassfish.jaxb
jaxb-runtime
2.2.11
junit
junit
test
4.12
org.codehaus.mojo
xml-maven-plugin
1.0.2
transform
process-sources
${project.build.directory}/xjc-c/META-INF
${project.build.directory}/xjc-c/META-INF
sun-jaxb.episode
src/main/xslt/removeJaxbSchemaBindings.xslt
org.jvnet.jaxb2.maven2
maven-jaxb2-plugin
0.13.3
xjc-c
generate
generate-sources
test.c
${project.build.directory}/xjc-c
c.xsd
xjc-a
generate
generate-resources
test.a
${project.build.directory}/xjc-a
a.xsd
${project.build.directory}/xjc-c/META-INF
sun-jaxb.episode
xjc-b
generate
generate-resources
test.b
${project.build.directory}/xjc-b
b.xsd
${project.build.directory}/xjc-c/META-INF
sun-jaxb.episode