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



我目前正在将一个项目从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(srcmainjavaorgexamplebusToyota.java:7).
This problem is related to the following location:
At org.example.car.Toyota(srcmainjavaorgexamplecarToyota.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(srcmainjavaorgexamplebusToyota.java:7).
This problem is related to the following location:
At org.example.car.Toyota(srcmainjavaorgexamplecarToyota.java:7).
Note: Writing D:WorkProjectsGitxsd-generation-testschemasMETA-INFJAXBepisode_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>

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

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

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

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

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

最新更新