0

I am trying to get the coverage of a few test cases related to an open source project of which I got the jar file.

I'm using maven as automatic build tool and the jacoco plugin for maven to get the coverage of the tests on the project.

What I would like to achieve is the same output that I can get with the command:

-jar jacococli.jar report jacoco.exec \
                       --classfiles <project.jar> \
                       --sourcefiles <myTestsDirectory> \
                       --html <jacocoReportsDirectory> \
                       --xml <jacocoReportsDirectory>coverageFile.xml \
                       --csv <jacocoReportsDirectory>coverageFile.csv

When I run this from the commandline I can retrieve the report for the coverage computed on the whole project (the jar).

I don't know how to insert this command (or something that can get me the same output) in the pom. I tried to put it in the surefire plugin argLine (with the -javaagent command) but this got me nowhere.

This is where I got so far:

<build>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>src/test</testSourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.7</version>

                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                       </goals>
                   </execution>
                   <execution>
                       <id>report</id>
                       <phase>prepare-package</phase>
                       <goals>
                           <goal>report</goal>
                       </goals>
                   </execution>
                   <execution>
                       <id>post-unit-test</id>
                       <phase>test</phase>
                       <goals>
                           <goal>report</goal>
                       </goals>
                       <configuration>
                           <dataFile>target/coverage-reports/jacoco.exec</dataFile>
                           <outputDirectory>target/coverage-reports/jacoco-reports</outputDirectory>
                     </configuration>
                   </execution>
               </executions>
           </plugin>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>3.0.0-M4</version>
               <configuration>
                   <argLine>
                       -javaagent:lib/jacocoagent.jar=destfile=target/coverage-reports/jacoco.exec
                   </argLine>
                   <reportsDirectory>target/coverage-reports/surefire-reports</reportsDirectory>
               </configuration>
           </plugin>
       </plugins>
   </build>

Giving the whole src directory as the sourceDirectory gets my test cases as the target of the coverage computation and I don't want that.

Note: there are no java classes in src to which the tests are targeted, so there's no need to put that as sourceDirectory. I assume this has to point to the jar somehow.

lilith
  • 63
  • 5
  • Why are you changing the defaults like `src src/test` that does not make sense... keep the conventions. If you have unit tests why don't you have classes in `src/main/java`? – khmarbaise Mar 23 '22 at 19:12
  • @khmarbaise cause the classes I want to test are in a jar file – lilith Mar 24 '22 at 08:08
  • Why? Unit tests belong to the module they are in... As I wrote. Production code `src/main/java` unit tests `src/test/java` makes things easier... – khmarbaise Mar 24 '22 at 08:10

0 Answers0