jaxb2-maven-plugin generating package-info.java with xmlns prefixes
Asked Answered
F

1

7

I want to generate java classes with the jaxb2-maven-plugin. I am using the following configuration:

pom.xml:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.5.0</version>
    <executions>
        <execution>
            <id>SomeID</id>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration>
                <extension>true</extension>
                <clearOutputDir>true</clearOutputDir>
                <sources>
                    <source>src/main/xsd/schema.xsd</source>
                </sources>
                <noGeneratedHeaderComments>true</noGeneratedHeaderComments>
            </configuration>
        </execution>
    </executions>
</plugin>

schema.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://my.target.namespace/uri" 
    xmlns="http://my.target.namespace/uri" 
    elementFormDefault="qualified"
    attributeFormDefault="unqualified"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:h="http://my.uri.for.prefix.h"
    xmlns:f="http://my.target.namespace/uri">
    
    <xsd:import namespace="http://my.uri.for.prefix.h" schemaLocation="schema2.xsd"/> 
    
    <xsd:complexType name="FooType">
        <xsd:sequence>
            <xsd:element ref="h:something" minOccurs="0" maxOccurs="1"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="FooType" type="FooType" />
 
</xsd:schema>

The Jaxb2 plugin is generating me the following package-info.java:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://my.target.namespace/uri", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ...;

But, what I want to get is this:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://my.target.namespace/uri", xmlns = {
        @XmlNs(prefix="f", namespaceURI="http://my.target.namespace/uri"),
        @XmlNs(prefix="h", namespaceURI="http://my.uri.for.prefix.h")
}, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ...;

import javax.xml.bind.annotation.XmlNs;

The prefixes are missing in my generated file. How to do that? I tried already to create a binding file but this didn't worked how I expected.

Fustian answered 24/6, 2020 at 12:25 Comment(4)
I tried already this post: #892798Fustian
Did you ever manage to solve this?Rancho
Not really, my workaround is to create the package-info.java manually.Fustian
@Fustian did you find a better solution?Convent
R
3

Please see this answer on how to solve this problem: https://mcmap.net/q/1073477/-how-to-customize-package-info-java-generated-by-jaxb2

The answer is very well written and easy to follow. Basically you will have to:

  1. Drop jaxb2-maven-plugin in favour of maven-jaxb2-plugin.
  2. Include the jaxb2-namespace-prefix dependency and provide the <arg>-Xnamespace-prefix</arg>.
  3. Write a new bindings.xml file which is only a few lines long.

Your POM file will become more verbose, but it is worth it to have a package-info.java generated the way you require.

As a bonus, there are a heap of additional plugins and dependencies related to maven-jaxb2-plugin that provide extra features. One that I found helpful was jaxb2-rich-contract-plugin that gave the ability to generate builders and make the generated classes immutable*.

* Well, not strictly speaking immutable (as it just changes the setter methods to be package private), but enough to make them feel safer.

Rancho answered 29/7, 2021 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.