How to customize package-info.java generated by JAXB2
Asked Answered
B

2

10

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?

Bauhaus answered 12/1, 2012 at 14:57 Comment(0)
C
13

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>
Cohlette answered 30/5, 2012 at 7:58 Comment(11)
What if I want to override @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ech.ch/xmlns/eCH-0007/3" to @javax.xml.bind.annotation.XmlSchema(namespace = "" ? How can that be done?Longrange
@icedek, if you change the xml namespace, you are changing the very definition of your XSD : even though xml structures would remain identicals, they would not be compatibles. If you need to do that, why not modify the XSD itself ?Cohlette
Does anyone know if the same thing can be achieved with the jaxws-maven-plugin?Orifice
@Orifice for jaxws-maven-plugin use -B-Xnamespace-prefix.Mimas
"unrecognized parameter -Xnamespace-prefix"Turd
@BlessedGeek, did you add the jaxb2-namespace-prefix dependency ? Note that it's a dependency of the maven-jaxb2-plugin plugin itself.Cohlette
<namespace:prefix name="eCH-0007" /> gives me org.xml.sax.SAXParseException: The prefix "namespace" for element "namespace:prefix" is not bound.Fetlock
Make sure to define xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix in header. Full example here : github.com/Siggen/jaxb2-namespace-prefix.Cohlette
What if I have multiple XSDs in a directory and all of them should have the same namespace prefix?Publicly
Can I put directory name in schemaLocation? <jxb:bindings schemaLocation="dir_name">Publicly
@Publicly To have multiple XSDs in a directory all sharing the same namespace prefix, I guess you'll have to repeat the binding definition for each XSD and each time specify the same prefix. I didn't try it though. And for using a directory as argument of schemaLocation (kind of a wildcard criteria), it's more related to the way jxb binding works, and I don't really know.Cohlette
F
2

You can customise namespace by guiding JAX-B to generate the package-info.java and set the target namespace needed in your XSD.

<xs:schema version="1.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xsi:schemaLocation="http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd"
    targetNamespace="yourTargetNameSpace"
    xmlns="yourTargetNameSpace"
    elementFormDefault="qualified">
Fin answered 14/2, 2013 at 8:52 Comment(1)
This is the easiest answer if you have access to the xsd.Sorcha

© 2022 - 2024 — McMap. All rights reserved.