trzeci
trzeci

Reputation: 61

JAXB: mapping namespace to package with maven plugin

Let's assume I have a.xsd file in resources/a directory and b.xsd file in resources/b directory. a.xsd and b.xsd have their own namespaces: http://a.com and http://b.com. And finally, a.xsd imports b.xsd.

I'd like to configure maven-jaxb2-plugin to generate A.java in package com.a and B.java in package com.b

  1. How to bind all xsd files from one namespace to one package and all xsd files from another namespace to another package.
  2. How to pass two different directories to maven-jaxb2-plugin. Multiple executions do not work for me.

Upvotes: 4

Views: 4640

Answers (1)

lexicore
lexicore

Reputation: 43651

Would be better if you ask these questions separately.

First question - just use multiple jaxb:schemaBindings.

<jaxb:bindings schemaLocation="a.xsd" node="/xs:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="com.a"/>
    </jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="b.xsd" node="/xs:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="com.b"/>
    </jaxb:schemaBindings>
</jaxb:bindings>

Second question - multiple executions or do some file-moving postprocessing on your own. Why do multiple executions not work for you?

Update: You say you're interested in a per-namespace, not per-file solution.

First, it doesn't really matter that much, at the end it's a per-namespace solution in any case. If you bind via schemaLocation, XJC associates schemaBindings with the target namespace of that schema. You don't have to customize every file of that schema and you can't define two packages for one namespace.

Second, you can use SCD bindings instead:

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

Upvotes: 7

Related Questions