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

GitLab流水线构建失败:Avro插件缺失Schema$Parser类求助

问题:GitLab流水线构建失败,Avro插件缺失核心类

我们在项目中新增Kafka自动化功能后,本地使用新消费者账号可正常拉取并消费Topic消息,但推送代码至GitLab流水线后构建失败,报错如下:

"[ERROR] Failed to execute goal org.apache.avro:avro-maven-plugin:1.11.3:schema (default) on project: Execution default of goal org.apache.avro:avro-maven-plugin:1.11.3:schema failed: A required class was missing while executing org.apache.avro:avro-maven-plugin:1.11.3:schema: org/apache/avro/Schema$Parser"

尝试多种方法未解决,求助:是否需要修改.gitlab-ci.yml?

项目POM.xml相关配置(用于avsc文件生成Java类)

<!-- Kafka客户端依赖 -->
<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>3.7.0</version>
</dependency>
<dependency>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro</artifactId>
    <version>${avro.version}</version>
    <scope>compile</scope>
</dependency>
<!-- Confluent Avro序列化器及Schema Registry客户端 -->
<dependency>
    <groupId>io.confluent</groupId>
    <artifactId>kafka-avro-serializer</artifactId>
    <version>${confluent.version}</version>
</dependency>
<dependency>
    <groupId>io.confluent</groupId>
    <artifactId>kafka-schema-registry-client</artifactId>
    <version>${confluent.version}</version>
</dependency>

</dependencies>

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro-maven-plugin</artifactId>
            <version>${avro.version}</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>schema</goal>
                    </goals>
                    <configuration>
                        <sourceDirectory>${project.basedir}/src/avro/</sourceDirectory>
                        <outputDirectory>${project.build.directory}/generated-sources/avro</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

.gitlab-ci.yml配置内容

variables:
  MAVEN_OPTS: >-
    -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository

default:
  image: $CONTAINER_PATH/maven-container

cache:
  key: "$CI_JOB_NAME"
  paths:
    - .m2/repository

build-project:
  stage: build
  script:
    - mvn test-compile
    - mvn generate-sources -U -X

build-nightly:
  stage: build
  tags:
    - special-tag
  services:
    - selenium/standalone-chrome
  script:
    - mvn test -Dgroups=$SMOKE_GROUP
  allow_failure: false
  rules:
    - if: $SMOKE_GROUP

pages:
  stage: deploy
  script:
    - mvn site
    - mv target/site public
  artifacts:
    paths:
      - public
  rules:
    - if: $CI_COMMIT_REF_PROTECTED == "true"

解决方案

问题根源

报错提示org/apache/avro/Schema$Parser类缺失,本质是Avro Maven插件运行时无法获取Avro核心依赖。本地构建正常是因为IDE会自动补全插件类路径,但Maven在CI环境中严格分离插件与项目的类路径,必须显式配置插件依赖。

修复步骤

1. 补全POM中Avro插件的依赖配置

修改Avro插件节点,添加插件专属的依赖块,确保插件运行时能找到Schema类:

<plugin>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro-maven-plugin</artifactId>
    <version>${avro.version}</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>schema</goal>
            </goals>
            <configuration>
                <sourceDirectory>${project.basedir}/src/avro/</sourceDirectory>
                <outputDirectory>${project.build.directory}/generated-sources/avro</outputDirectory>
            </configuration>
        </execution>
    </executions>
    <!-- 新增插件依赖,解决运行时类缺失问题 -->
    <dependencies>
        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro</artifactId>
            <version>${avro.version}</version>
        </dependency>
    </dependencies>
</plugin>

2. 优化GitLab CI构建命令

当前build-project阶段的命令存在重复执行问题,mvn test-compile会提前触发编译,但generate-sources应该在编译前执行。直接使用mvn clean compile即可自动触发源码生成阶段,同时确保依赖完整拉取:

build-project:
  stage: build
  script:
    - mvn clean compile -U -X

3. 统一CI缓存配置(可选)

当前缓存按任务名分组,可能导致不同任务依赖缓存不共享。统一缓存key可减少重复下载:

cache:
  key: "$CI_PROJECT_PATH"
  paths:
    - .m2/repository

额外检查

  • 确认POM的<properties>中已正确定义${avro.version},且版本与插件版本一致(如1.11.3)
  • 检查GitLab使用的Maven容器是否配置了可靠的镜像源,避免依赖拉取超时或失败

内容的提问来源于stack exchange,提问作者sai sruthi keerthi narumanchi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 04:09:52