5

Basically I need jacoco only instrument the tests part, but is instrumententing the entire pom.xml, and the report came with everything (Data from “oracle.jdbc.driver”, “com.mysql.jdbc”… etc.)

I've been trying for a couple of days with almost everything. But I have not succeeded so far

Notice here how jacoco:instrument instrument the entire 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

And my tests run in

[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

This is my maven flow:

[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

Any real example here would be great In "Jacoco default-instrument" for run only tests part. It is that possible?

<execution>
      <id>default-instrument</id>
      <goals>
         <goal>instrument</goal>
      </goals>
      <configuration>
      <!-- any real example here? Notice maven's behavior above -->
      </configuration>
  </execution>
Didier R. G.
  • 437
  • 5
  • 11

2 Answers2

2

Both jacoco:instrument and jacoco:report operate with target/classes, because this is the classes that are executed and whose coverage is measured.

If you place in target/classes more classes than in src, then without corresponding inclusions / exclusions they will also be instrumented and reported.

Please note that exclusion of classes from instrumentation is not enough to exclude classes from report - quoting JaCoCo FAQ:

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

So please make sure that you properly configured execution of both instrument and report goals. Maven allows many different ways to configure different executions of different goals, here is just one of examples:

<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>
Godin
  • 9,801
  • 2
  • 39
  • 76
  • 1
    That didn't worked for me w/ Jacoco version 0.8.5. But declaring excludes on top level of plugin (plugin/configuration/excludes/exclude ... ) worked for me. – Wlad Aug 02 '20 at 13:00
1

You can use the <includes> tag and put in there your package default.

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

reference

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68