Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize package-info.java generated by JAXB2

Tags:

maven

jaxb2

I'm using

<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>

to generate Java classes from XSD files.

I've added

<args>-npa</args>

so, the plugin doesn't generate anymore package-info.java, but with this option the generated java classes are different (namespace is added to every element).

So, I cannot customize the namespace using package-info.java.

How can I use a custom namespace without modifying manually generated files?

like image 652
ovi2ut Avatar asked Feb 03 '23 09:02

ovi2ut


1 Answers

You may use the namespace-prefix plugin from jaxb2-common project (disclaimer : I wrote it) :

https://github.com/Siggen/jaxb2-namespace-prefix

This is a xjc pluging which allows to define namespace -> prefix mappings within the bindings.xml file :

<jxb:bindings schemaLocation="eCH-0007-3-0.xsd">
    <jxb:schemaBindings>
        <jxb:package name="ch.ech.ech0007.v3" />
    </jxb:schemaBindings>
    <jxb:bindings>
        <namespace:prefix name="eCH-0007" />
    </jxb:bindings>
</jxb:bindings>

Which will results in the following package-info.java file being generated (mind the added XmlNs annotation) :

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ech.ch/xmlns/eCH-0007/3", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = {
    @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://www.ech.ch/xmlns/eCH-0007/3", prefix = "eCH-0007-3")
})
package ch.ech.ech0007.v3;

Your pom.xml would look like :

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.0</version>
    <configuration>
        <schemaDirectory>src/main/resources</schemaDirectory>
        <catalog>src/main/resources/catalog.xml</catalog>
        <schemaIncludes>
            <include>*.xsd</include>
        </schemaIncludes>
        <bindingDirectory>src/main/resources</bindingDirectory>
        <bindingIncludes>
            <include>bindings.xml</include>
        </bindingIncludes>
        <args>
            <arg>-extension</arg>
            <arg>-Xnamespace-prefix</arg>
        </args>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-namespace-prefix</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</plugin>
like image 162
Siggen Avatar answered Feb 05 '23 16:02

Siggen