jaxb2-maven-plugin 在生成 xsd 时忽略命名空间 - 两个 类 具有相同的 XML 类型

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

我目前正在将项目从 Java 8 迁移到 Java 11。版本更改破坏了我们的 XSD 一代。

我们正在使用 org.codehaus.mojo:jaxb2-maven-plugin:2.5.0 从注释为 类 的 JAXB 生成 XSD .但它似乎忽略了为不同包定义的命名空间,因为它打印出以下错误:

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

有没有人遇到过这个问题并且能够为我提供解决方案?


以下示例是重现错误的最小配置:

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 包“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 包“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 {
}

原来 jaxb2-maven-plugin 不支持最新形式的 jakarta.xml.bind 导入。

因此您可以降级到仍然具有 javax.xml.bind 导入的依赖项,或者您可以切换到支持新 jaxb-API.[=11= 的插件的分叉版本]

分叉插件回购:https://github.com/evolvedbinary/mojohaus-jaxb-maven-plugin/