Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 298
Didier R. G. Avatar asked Oct 16 '22 10:10

Didier R. G.


1 Answers

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>
like image 87
Godin Avatar answered Nov 09 '22 12:11

Godin