Home > Enterprise >  jaxb2-maven-plugin ignores namespace when generating xsd - Two classes have the same XML type
jaxb2-maven-plugin ignores namespace when generating xsd - Two classes have the same XML type

Time:10-26

I am currently migrating a project from Java 8 to Java 11. The version change broke our XSD generation.

We are using the org.codehaus.mojo:jaxb2-maven-plugin:2.5.0 to generate the XSD from JAXB annotated classes. But it seems to be ignoring the namespace defined for the different packages, as it prints the following error:

Two classes have the same XML type name "toyota". Use @XmlType.name and @XmlType.namespace to assign them different names.
    This problem is related to the following location:
        at org.example.bus.Toyota(src\main\java\org\example\bus\Toyota.java:7).
    This problem is related to the following location:
        At org.example.car.Toyota(src\main\java\org\example\car\Toyota.java:7).
Error: two classes have the same XML type name "toyota". Use @XmlType.name and @XmlType.namespace to assign them different names.
    This problem is related to the following location:
        at org.example.bus.Toyota(src\main\java\org\example\bus\Toyota.java:7).
    This problem is related to the following location:
        At org.example.car.Toyota(src\main\java\org\example\car\Toyota.java:7).
Note: Writing D:\Work\Projects\Git\xsd-generation-test\schemas\META-INF\JAXB\episode_schemagen.xjb

Has anyone had this problem and is able to provide me with a solution?


The following example is a minimal configuration to repoduce the error:

pom.xml

<dependencies>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.5.0</version>
                <executions>
                    <execution>
                        <id>schemagen</id>
                        <goals>
                            <goal>schemagen</goal>
                        </goals>
                        <phase>generate-resources</phase>
                    </execution>
                </executions>
                <configuration>
                    <sources>
                        <source>src/main/java/org/example/car</source>
                        <source>src/main/java/org/example/bus</source>
                    </sources>
                    <outputDirectory>schemas</outputDirectory>
                    <transformSchemas>
                        <transformSchema>
                            <uri>https://car.example.org/</uri>
                            <toFile>car.xsd</toFile>
                        </transformSchema>
                        <transformSchema>
                            <uri>https://bus.example.org/</uri>
                            <toFile>bus.xsd</toFile>
                        </transformSchema>
                    </transformSchemas>
                </configuration>
            </plugin>
        </plugins>
    </build>

Class in Package "org.example.bus":

package org.example.bus;

import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "toyota", namespace = "https://bus.example.org/")
public class Toyota {
}

Class in Package "org.example.car":

package org.example.car;

import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "toyota", namespace = "https://car.example.org/")
public class Toyota {
}

CodePudding user response:

Turns out the jaxb2-maven-plugin does not support the jakarta.xml.bind imports in its latest form.

So you can either downgrade to a dependency that still has the javax.xml.bind imports or you can switch to a forked version of the plugin that supports the new jaxb-API.

Forked Plugin Repo: https://github.com/evolvedbinary/mojohaus-jaxb-maven-plugin/

  • Related