首页 > 解决方案 > JaCoCo(离线仪器)在乐器分析整个 pom.xml。但我只需要测试部分

问题描述

基本上我只需要 jacoco 对测试部分进行检测,但是对整个 pom.xml 进行检测,并且报告附带了所有内容(来自“oracle.jdbc.driver”、“com.mysql.jdbc”等的数据)

我已经尝试了几天几乎所有的东西。但到目前为止我还没有成功

请注意这里jacoco:instrument如何检测整个 pom.xml

[INFO] --- jacoco-maven-plugin:0.8.4:instrument (default-instrument) @ myApp ---
...
[DEBUG]   (f) project = MavenProject: com.firstPackage.tdz:myApp:X.1.0 @ C:\rootFolder\my_app\server\MyApp\pom.xml

我的测试运行在

[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ myApp ---
[DEBUG] Source directories: [C:\rootFolder\my_app\server\myApp\tests\src]
[DEBUG] Classpath: [C:\devel\my_app\server\myApp\target\test-classes

这是我的 Maven 流程:

[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ myApp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3582 source files to c:\rootFolder\my_app\server\myApp\target\classes
...
[INFO] --- aspectj-maven-plugin:1.3:compile (default) @ myApp ---
...
[INFO] --- jacoco-maven-plugin:0.8.4:instrument (default-instrument) @ myApp ---
...
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ myApp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 326 source files to c:\rootFolder\my_app\server\myApp\target\test-classes
...
[INFO] --- aspectj-maven-plugin:1.3:test-compile (default) @ myApp ---
...
[INFO] --- maven-surefire-plugin:2.15:test (default-test) @ myApp ---

... finally

[INFO] --- jacoco-maven-plugin:0.8.4:restore-instrumented-classes (default-restore-instrumented-classes) @ myApp ---
...
[INFO] --- jacoco-maven-plugin:0.8.4:report (default-report) @ myApp ---
[INFO] Loading execution data file c:\devel\my_app\server\MyApp\target\jacoco.exec
[INFO] Analyzed bundle 'myApp' with 5562 classes

这里的任何真实示例在“Jacoco default-instrument”中对于仅运行测试部分都会很棒。这可能吗?

<execution>
      <id>default-instrument</id>
      <goals>
         <goal>instrument</goal>
      </goals>
      <configuration>
      <!-- any real example here? Notice maven's behavior above -->
      </configuration>
  </execution>

标签: javajacocojacoco-maven-plugin

解决方案


和都jacoco:instrumentjacoco:report一起操作target/classes,因为这是执行的类并且其覆盖范围被测量。

如果您放置的target/classes课程多于src,那么如果没有相应的inclusions/exclusions他们也将被检测和报告。

请注意,排除课程instrumentation不足以排除课程report- 引用JaCoCo 常见问题解答

报告生成器无法区分该类是从检测中排除还是未执行

因此,请确保您正确配置instrumentreport目标的执行。Maven 允许许多不同的方式来配置不同目标的不同执行,这里只是示例之一:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <execution>
    <goals>
      <goal>instrument</goal>
      <goal>report</goal>
    </goals>
    <configuration>
      <!-- this configuration affects this "execution" of "instrument" and "report" goals -->
      <excludes>*</excludes>
    </configuration>
  </execution>

推荐阅读