How can I add a suffix to my jaxb generated java beans using jaxb2-maven-plugin

129 Views Asked by At

From I third party I imported xsd's and a wsdl as contracts for their services. I am generating the beans with the maven-jaxb2-plugin . Because of naming collisions in my project I want to add suffixes to the bean names generated.

This is my plugin configuration in the :

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>${jaxb2.maven.version}</version>
                <configuration>
                    <xjbSources>
                        <xjbSource>src/main/resources/bindings.xjb</xjbSource>
                    </xjbSources>
                    <sources>
                        <source>src/main/resources/xsd</source>
                    </sources>
                </configuration>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

And this is my bindings.xjb file:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        version="3.0">

    <jaxb:globalBindings>
        <!-- All beans should extend this base class: -->
        <xjc:superClass name="my.project.xml.MyXmlObject"/>
        <!-- No need for bigint -->
        <jaxb:javaType name="long" xmlType="xs:nonNegativeInteger"
                       parseMethod="my.project.xml.TypeConverter.parseLong"
                       printMethod="my.project.xml.TypeConverter.printLong"/>
    </jaxb:globalBindings>

</jaxb:bindings>

The project builds fine like this, and beans are generated.

I have found some code like, but it gives errors:

<jaxb:schemaBindings>
            <jaxb:package name="my.project"/>
            <jaxb:nameXmlTransform>
                <jaxb:elementName prefix="My" suffix="Dto"/>
                <jaxb:typeName prefix="My" suffix="Dto"/>
            </jaxb:nameXmlTransform>
        </jaxb:schemaBindings>

I get the error:

com.sun.istack.SAXParseException2: The "jaxb:schemaBindings" customization is not associated with any schema element.

How can I fix this?

1

There are 1 best solutions below

9
Laurent Schoelens On

My guess is you're missing the schemaLocation="myxsd.xsd" in the schemaBindings section

For example, according to your configuration, you should try to add

<jaxb:bindings schemaLocation="xsd/myxsd.xsd">
  <jaxb:schemaBindings>
    <jaxb:package name="mypackage"/>
    <jaxb:nameXmlTransform>
      <jaxb:elementName prefix="My" suffix="Dto"/>
      <jaxb:typeName prefix="My" suffix="Dto"/>
    </jaxb:nameXmlTransform>
  </jaxb:schemaBindings>
</jaxb:bindings>

Tested against a simple test project with package set to po and got the following result

Regards

EDIT 13th february 2024

You can in fact use schemaLocation="*" to apply to every file in the current XJC context. But this option also tries to apply to binding file, it then fails.
Filling new issue