Yes, this is possible, keywords are:
- Separate schema compilation with episodes to avoid generating classes which are already generated
- Catalogs to override schema location including pointing to the schema inside the Maven artifact
Required disclaimer: I'm the author of the maven-jaxb2-plugin
which provides these features.
Update:
I tried to use separate schema compilation, but unfortunately I can't
undersant it. Should I create separate jar only for xsd file and use
it as a dependency ? Now I have dependency to the whole project A
which contains generated classes and xsd file
This is enough. You need a JAR with generated classes, XSD files and META-INF/sun-jaxb.episode
. All of this is normally generated by default.
Then you can use this JAR as an "episode". XJC will use sun-jaxb.episode
resource as binding file automatically, and this file basically says something like "don't generate anything for the schema in namespace X, use the following classes instead." This is one part which allows you to avoid redundant generation of classes. This works mostly, but sometimes XJC still generates a few things - they can be safely deleted.
The other thing is that you want to use schemas from the JAR. This can be done with catalogs and a special entity resolver which is by default delivered by the maven-jaxb2-plugin
.
So just use any schemaLocation
and then rewrite it in the catalog file:
REWRITE_SYSTEM
"https://acme.com/foo/a.xsd"
"maven:com.acme.foo:foo-a!/a.xsd"