You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Avro Maven Plugin无法重定义类型:依赖Schema导入问题求解决方案

解决Avro Maven插件中依赖Schema导入冲突问题

问题场景

在Quarkus项目中使用avro-maven-plugin(版本1.11.3),插件配置如下:

<plugin>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro-maven-plugin</artifactId>
    <version>1.11.3</version>
    <configuration>
        ...
    </configuration>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>schema</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/*.avsc</include>
                </includes>
                <imports>
                    <import>A.avsc</import>
                    <import>B.avsc</import>
                </imports>
            </configuration>
        </execution>
    </executions>
</plugin>

业务场景中B.avsc依赖A.avsc,但插件文档要求导入的文件不应互相引用,执行构建时出现报错:

[ERROR] Failed to execute goal io.quarkus.platform:quarkus-maven-plugin:3.6.6:dev (default-cli) on project : Unable to execute mojo: Execution null of goal org.apache.avro:avro-maven-plugin:1.11.3:schema failed: Can't redefine: -> [Help 1]

可行解决办法

  • 移除插件配置中的<imports>段,改用Schema内部的import语句
    无需在Maven插件中手动声明所有依赖的Schema,直接在B.avsc内部通过imports字段引入A.avsc,示例:

    {
      "type": "record",
      "name": "B",
      "namespace": "com.example",
      "imports": ["A.avsc"],
      "fields": [
        {"name": "aField", "type": "A"}
      ]
    }
    

    插件会自动解析Schema之间的依赖关系,避免重复定义问题。

  • 仅导入最上层依赖的Schema(保留<imports>的情况)
    如果必须保留插件的<imports>配置,只需要导入直接使用的顶层Schema(即B.avsc)即可,插件会自动递归处理其依赖的A.avsc,无需重复导入:

    <imports>
        <import>B.avsc</import>
    </imports>
    
  • 清理构建缓存后重新执行
    执行mvn clean generate-sources命令,清除之前生成的Java类和构建缓存,避免缓存中的旧代码导致重复定义冲突。

  • 确认Schema文件路径正确性
    确保A.avsc和B.avsc的文件位置被插件的<includes>规则覆盖(比如都在src/main/resources或其子目录下),插件才能正确识别并解析依赖关系。

内容的提问来源于stack exchange,提问作者klemen kobau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:50:12