JAXB binding multiple files with same namespace to same package

喜你入骨 提交于 2019-12-20 07:22:00

问题


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?



来源:https://stackoverflow.com/questions/28041665/jaxb-binding-multiple-files-with-same-namespace-to-same-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!