使用org.jvnet.ogc/gml-v_3_2_1 episode时maven-jaxb2-plugin生成代码无法编译
我在Maven构建过程中,使用maven-jaxb2-plugin为AIXM XSD生成JAXB类,该XSD依赖OGC GML 3.2.1、xlink及ISO 19139 schema。借助ogc-schemas项目的预构建绑定和episode文件,成功生成代码但编译失败,报错:
aixm.ApronElementTimeSliceType is not abstract and does not override abstract method createNewInstance() in org.jvnet.jaxb2_commons.lang.CopyTo2
该抽象方法定义在net.opengis.gml.v_3_2_1.AbstractTimeSliceType中,继承链如下:
aixm.ApronElementTimeSliceType (项目生成) extends aixm.AbstractAIXMTimeSliceType (项目生成) extends aixm.AbstractAIXMTimeSliceBaseType (项目生成) extends net.opengis.gml.v_3_2_1.AbstractTimeSliceType (来自org.jvnet.ogc:gml-v_3_2_1)
我需要让maven-jaxb2-plugin自动为生成的非抽象类实现createNewInstance()方法,实现逻辑为返回new CLASS_NAME_HERE()。
我的pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId><!-- SNIP --></groupId> <artifactId><!-- SNIP --></artifactId> <version><!-- SNIP --></version> <properties> <maven.compiler.source>19</maven.compiler.source> <maven.compiler.target>19</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.7</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.jvnet.ogc</groupId> <artifactId>gml-v_3_2_1</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.hisrc.w3c</groupId> <artifactId>xlink-v_1_0</artifactId> <version>1.4.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.15.2</version> <executions> <execution> <id>fixm</id> <!-- SNIP... this execution is not relevant to this question... --> </execution> <execution> <id>aixm</id> <goals> <goal>generate</goal> </goals> <configuration> <extension>true</extension> <generatePackage>aixm</generatePackage> <generateDirectory>${project.basedir}/src/main/generated-sources-aixm</generateDirectory> <removeOldOutput>true</removeOldOutput> <schemaDirectory>src/main/xsd/aixm</schemaDirectory> <schemaIncludes> <include>**/*.xsd</include> </schemaIncludes> <bindingDirectory>src/main/xjb/aixm</bindingDirectory> <bindingIncludes> <include>**/*.xjb</include> </bindingIncludes> <episodes> <episode> <groupId>org.jvnet.ogc</groupId> <artifactId>gml-v_3_2_1</artifactId> </episode> <episode> <groupId>org.hisrc.w3c</groupId> <artifactId>xlink-v_1_0</artifactId> </episode> </episodes> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.6</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
自定义绑定文件(解决ObjectFactory冲突)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <jaxb:bindings version="2.1" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"> <jaxb:bindings schemaLocation="../../xsd/aixm/AIXM_Features.xsd"> <jaxb:bindings node="/xs:schema/xs:element[@name='Curve']"> <jaxb:factoryMethod name="CurveAixm"/> </jaxb:bindings> <jaxb:bindings node="/xs:schema/xs:element[@name='Point']"> <jaxb:factoryMethod name="PointAixm"/> </jaxb:bindings> <jaxb:bindings node="/xs:schema/xs:element[@name='Surface']"> <jaxb:factoryMethod name="SurfaceAixm"/> </jaxb:bindings> </jaxb:bindings> <jaxb:bindings schemaLocation="http://schemas.opengis.net/iso/19139/20070417/gco/gcoBase.xsd"> <jaxb:bindings node="/xs:schema/xs:element[@name='AbstractObject']"> <jaxb:factoryMethod name="AbstractObjectIso19139"/> </jaxb:bindings> </jaxb:bindings> </jaxb:bindings>
这个问题的核心是ogc-schemas中的类依赖jaxb2-basics插件的CopyTo2接口,但你的生成流程未启用该插件自动实现相关方法。按以下步骤修改配置即可解决:
- 给maven-jaxb2-plugin添加jaxb2-basics依赖
在plugin的<dependencies>节点中新增:
<dependency> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics</artifactId> <version>0.12.0</version> </dependency>
- 在aixm执行配置中启用CopyTo扩展
在<execution id="aixm">的<configuration>里新增:
<plugins> <plugin> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics</artifactId> <version>0.12.0</version> </plugin> </plugins> <args> <arg>-Xcopy-to</arg> </args>
修改后,maven-jaxb2-plugin会自动为生成的类实现CopyTo2接口的所有方法,包括createNewInstance(),实现逻辑就是返回当前类的新实例。注意保持jaxb2-basics版本(0.12.0)与maven-jaxb2-plugin版本(0.15.2)兼容。
内容的提问来源于stack exchange,提问作者Joe Crivello

