JAXB binding multiple files with same namespace to

2020-05-06 16:41发布

问题:

I'm having a schema hierarchy like this:

common
   |---legacy
   |      |---legacy.xsd xmlns="http://common/legacy"
   |      |---other.xsd  xmlns="http://common/legacy"
   |      '---....xsd    xmlns="http://common/legacy"
   |---send
          |---file.xsd xmlns="http://common/send"
          '---text.xsd xmlns="http://common/send"
          '---....xsd  xmlns="http://common/send"

All files in one folder have the same namespace.

Now I want to map the namespaces to specific java packages (I cannot change the namespace).

I found a solution to bind a schema to a package. But then I would have to create one entry per xsd-file:

<jaxb:bindings schemaLocation="./common/legacy/legacy.xsd">
    <jaxb:schemaBindings>
        <jaxb:package name="com.company/legacy"/>
    </jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="./common/legacy/other.xsd">
    <jaxb:schemaBindings>
        <jaxb:package name="com.company/legacy"/>
    </jaxb:schemaBindings>
</jaxb:bindings>
.....

Is there a way to directly define a binding between namespace and a package name?

The other way would be to define the package in maven:

<plugin>
 <groupId>org.jvnet.jaxb2.maven2</groupId>
 <artifactId>maven-jaxb2-plugin</artifactId>
 <configuration>
  <generatePackage>com.company/legacy</generatePackage>
 </configuration>
</plugin>

But then I would have to create one execution per folder, which is not really what I want.

回答1:

Disclaimer: I'm the author of maven-jaxb2-plugin.

XJC derives packages from namespaces, so you (normally) can't generate several packages for one namespace. There are a few tricks with jaxb:class/@ref but you don't want those as this may lead to all kinds of collisions.

So my suggestion would be to define multiple executions, one per distinct schema in the same namespace. You could use generatePackage although I generally advise to define package mappings in bindings instead.

When doing multiple executions, make sure you use distinct generateDirectory per execution.

By the way, why are you not comfortable with multiple bindings?