1

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221
Seega
  • 3,001
  • 2
  • 34
  • 48

2 Answers2

2

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?

Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • 2
    I just have the problem that I do not want to have one binding per file, which is a lot useless copy paste for >10 files. Also it generates an possible error source when a new file is added to the folder but not handled in the bindings. – Seega Jan 21 '15 at 07:24
  • @Seega Legacy makes you suffer, sorry. Multiple executions plus one binding per execution is what I'd personally do. – lexicore Jan 21 '15 at 07:26
  • Okay, what would you write in the binding? – Seega Jan 21 '15 at 13:43
  • @Seega One binding file per execution with `jaxb:schemaBindings/jaxb:package`. – lexicore Jan 21 '15 at 14:53
  • @lexicore, with this approach, how do you prevent imported (common) XSD's from being generated for each package? – Benny Bottema May 04 '18 at 11:35
  • @BennyBottema There will be a so-called *episode* file generated per execution. This is basically a bindings file. If you use it in another execution, classes from imported schema won't be generated again. There are few bugs in XJC so sometimes `ObjectFactory` is generated, I simply remove it. Here's a [huge project](https://github.com/highsource/ogc-schemas) which fully employs this mechanism. – lexicore May 04 '18 at 11:43
  • @lexicore Are you referring to [schema-parent/pom.xml](https://github.com/highsource/ogc-schemas/blob/master/schema-parent/pom.xml)? – Benny Bottema May 04 '18 at 11:57
  • @BennyBottema I refer to the whole project. It is built so that each schema/distinct version gets its own Maven module/artifact. Many schemas depend on each other and the project uses episode mechanism to handle this to avoid duplicate generation. – lexicore May 04 '18 at 12:06
  • @BennyBottema See [Using Episodes](https://github.com/highsource/maven-jaxb2-plugin/wiki/Using-Episodes) and [Modular Schema Compilation](https://github.com/highsource/maven-jaxb2-plugin/wiki/Modular-Schema-Compilation). There's quite a number of tricks in use. – lexicore May 04 '18 at 12:07
  • @lexicore Ahh yes, I thought modular schema compilation is used to import XSD from external dependency jars, I didn't know it also works with XSD's _within_ the same project as well (I'm struggling to find a working example config for that). Schema A and B with same namespace, output to different packages while reusing schema C of the same namespace. – Benny Bottema May 04 '18 at 12:26
  • @BennyBottema It can work within one project as well. You'll just need to include the generated episode file as bindings file. If you make a MCVE project [here](https://github.com/highsource/maven-jaxb2-plugin-support), I can help with it. – lexicore May 04 '18 at 12:30
  • @lexicore Thank you for the offer, but I don't have an example ready to go (my problem situation occurs in a proprietary code base). I did create a [new SO question](https://stackoverflow.com/questions/50176111/jaxb2-maven-plugin-reusing-commons-xsd-within-same-project) for this general situation. I'm guessing the answer should be very simple. – Benny Bottema May 04 '18 at 13:33
  • Please see https://stackoverflow.com/questions/50176111/maven-jaxb2-plugin-reusing-commons-xsd-within-same-project/50181542#50181542. – lexicore May 04 '18 at 18:58
0

Use scd syntax instead node="XPath" schemaLocation="path or *":

<jaxb:bindings scd="x-schema::tns" xmlns:tns="http://common/legacy">
    <jaxb:schemaBindings>
        <jaxb:package name="com.company.legacy"/>
    </jaxb:schemaBindings>
</jaxb:bindings>

SCD is not supported everywhere. Some XJC Customizations, like the jaxb2-basics, not working in jaxb:bindings.

kapitanrum
  • 131
  • 1
  • 4