<goal>instrument</goal> 中的 JaCoCo(离线检测)分析了整个 pom.xml。但我只需要测试部分

JaCoCo (Offline Instrumentation) in <goal>instrument</goal> analyzes the entire pom.xml. But I need only the tests part

基本上我需要 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

这里有任何真实的例子都很好 In "Jacoco default-instrument" for 运行 仅测试部分。有可能吗?

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

您可以使用 <includes> 标签并将您的包默认值放在那里。

<execution>
      <id>default-instrument</id>
      <goals>
         <goal>instrument</goal>
      </goals>
      <configuration>
           <includes> my/package/** </includes>
      </configuration>
</execution>

reference

两个 jacoco:instrument and jacoco:report 都与 target/classes 一起运行,因为这是执行的 类 并测量其覆盖率。

如果您在 target/classes 中放置的 类 多于在 src 中放置的内容,那么在没有对应的 inclusions / exclusions 的情况下,它们也会被检测和报告。

请注意,从 instrumentation 中排除 类 不足以从 report 中排除 类 - 引用 JaCoCo FAQ:

report generator cannot distinguish whether the class was excluded from instrumentation or not executed

因此,请确保您正确配置了 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>