如何为"jacoco-check"指定特定的软件包? [英] How to specify the specific packages for the 'jacoco-check'?

查看:126
本文介绍了如何为"jacoco-check"指定特定的软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是使用Maven构建的.我使用"Jacoco"插件执行质量检查.

My project is built using Maven. I use the 'Jacoco' plugin to perform quality checks.

对于一个项目,我想在线检查测试范围.我只想检查3个包裹的线路覆盖率.如何指定此检查?

For a project I would like to check the test coverage on line basis. I would like to check the line coverage only for only 3 packages. How can I specify this check?

我尝试包含"许多软件包,但这不起作用. 我还尝试包括根软件包级别,并排除许多其他软件包.也行不通.

I tried 'including' a number of packages, but that does not work. I also tried to include the root package level and exclude a number of other packages. Also not working.

如何检查包裹A,B和C?参见下面的示例:

How can I have the package A, B and C checked? See my example below:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.9</version>
    <executions>
      ...
      <execution>
        <id>jacoco-check</id>
        <goals>
          <goal>check</goal>
        </goals>
        <configuration>
          <rules>
            <rule>
              <element>PACKAGE</element>
              <includes>
                <include>nl.abc.xyz.package-a.**</include>
                <include>nl.abc.xyz.package-b.**</include>
                <include>nl.abc.xyz.package-c.**</include>
              </includes>
              ... 
              <limits>
                <limit>
                  <counter>LINE</counter>
                  <value>COVEREDRATIO</value>
                  <minimum>0.30</minimum>
                </limit>
              </limits>
            </rule>
          </rules>
        </configuration>
      </execution>
    </executions>
  </plugin>

推荐答案

includesruleexcludes大约是相应元素的name. 对于<element>PACKAGE</element>,它们与程序包名称有关. 因此

includes and excludes of rule are about name of the corresponding element. In case of <element>PACKAGE</element> they are about package name. And therefore

          <includes>
            <include>nl.abc.xyz.package-a.**</include>
            <include>nl.abc.xyz.package-b.**</include>
            <include>nl.abc.xyz.package-c.**</include>
          </includes>

例如匹配名为nl.abc.xyz.package-a.something的软件包,但与nl.abc.xyz.package-a不匹配.

Matches for example package named nl.abc.xyz.package-a.something, but doesn't match nl.abc.xyz.package-a.

给予

src/main/java/org/example/a/A.java

package org.example.a;

public class A {
}

src/main/java/org/example/a/B.java

package org.example.b;

public class B {
}

src/test/java/ExampleTest.java

public class ExampleTest {
  @org.junit.Test
  public void test() {
    new org.example.a.A();
  }
}

pom.xml

<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>org.example</groupId>
  <artifactId>example</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.2</version>
        <executions>
          <execution>
            <id>prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>report</id>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
          <execution>
            <id>check</id>
            <goals>
              <goal>check</goal>
            </goals>
            <configuration>
              <rules>
                <rule>
                  <element>PACKAGE</element>
                  <includes>
                    <include>org.example.b</include>
                  </includes>
                  <limits>
                    <limit>
                      <counter>LINE</counter>
                      <value>COVEREDRATIO</value>
                      <minimum>0.90</minimum>
                    </limit>
                  </limits>
                </rule>
              </rules>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

mvn verify的执行将按预期失败

[INFO] --- jacoco-maven-plugin:0.8.2:check (check) @ example ---
[INFO] Loading execution data file /private/tmp/j/target/jacoco.exec
[INFO] Analyzed bundle 'example' with 2 classes
[WARNING] Rule violated for package org.example.b: lines covered ratio is 0.00, but expected minimum is 0.90
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

以及在替换<include>org.example.*</include>上的<include>org.example.b</include>之后也将失败,并显示相同的消息,因为org.example.*org.example.b匹配.并且在<include>org.example.a</include>上进行替换后,将按预期成功.

and after replacement of <include>org.example.b</include> on <include>org.example.*</include> will also fail with the same message, because org.example.* matches org.example.b. And after replacement on <include>org.example.a</include> will succeed as expected.

这篇关于如何为"jacoco-check"指定特定的软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆