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

Azure构建流水线:多模块Maven项目JaCoCo测试目录排除方法

解决Azure Pipeline中JaCoCo排除测试类的正确配置

我之前也踩过JaCoCo过滤规则的坑——它可不是基于源文件路径来匹配的,而是编译后类的全限定名,所以你之前用-:*.src.test.java*.*这种路径写法肯定不生效。针对你的多模块Maven项目,这里给你两种靠谱的配置方案:

方式一:在父pom.xml中统一配置JaCoCo插件

多模块项目最省心的方式就是在父pom里配置,所有子模块自动继承规则:

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.10</version> <!-- 建议用最新稳定版 -->
      <executions>
        <!-- 准备测试代理,收集覆盖率数据 -->
        <execution>
          <id>prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <!-- 生成报告时排除测试类 -->
        <execution>
          <id>report</id>
          <goals>
            <goal>report</goal>
          </goals>
          <configuration>
            <excludes>
              <!-- 排除所有以Test结尾的测试类 -->
              <exclude>**/*Test.class</exclude>
              <!-- 适配JUnit 5常用的Tests结尾命名 -->
              <exclude>**/*Tests.class</exclude>
              <!-- 如果测试类都在单独的test包下,也可以用这个规则 -->
              <exclude>**/test/**/*.class</exclude>
            </excludes>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

方式二:在Azure Pipeline的Maven任务中配置过滤规则

如果不想动pom,直接在Pipeline的yaml里配置也可以,注意要用类名通配符:

steps:
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    goals: 'clean verify jacoco:report'
    codeCoverageToolOption: 'JaCoCo'
    # 逗号分隔多个排除规则
    codeCoverageClassFilter: '-:**/*Test.class, -:**/*Tests.class, -:**/test/**/*.class'

关键提醒

  • JaCoCo规则里,**代表任意层级的包结构,*代表任意字符;
  • 开头的-:表示排除匹配的类,:+是包含(默认包含所有);
  • 如果你的测试类有特殊命名(比如*TestCase.class),把对应的-:**/*TestCase.class加到规则里就行。

内容的提问来源于stack exchange,提问作者Dennis Crissman II

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 19:58:09