![](/img/trans.png)
[英]jaxb2-maven-plugin - how to turn off recursive directory traversal when generating xsd from classes
[英]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
有没有人遇到过这个问题并且能够为我提供解决方案?
以下示例是重现错误的最小配置:
<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;
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;
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 的插件的分叉版本。
分叉插件仓库: https : //github.com/evolvedbinary/mojohaus-jaxb-maven-plugin/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.