Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jacoco Maven multi module project coverage

Tags:

Seems like there are couple of questions, which are quite old and things changed from Java 8 support of Jacoco.

My Project contains following structure

pom.xml | | -----sub module A pom.xml | | -----sub module B pom.xml | | -----sub module C pom.xml 

I have configured the main pom like this

Main POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <groupId>com.test</groupId>     <artifactId>jacoco-multi</artifactId>     <version>0.0.1-SNAPSHOT</version>     <packaging>pom</packaging>     <modules>         <module>projectA</module>         <module>projectB</module>     </modules>      <properties>         <jacoco.version>0.5.7.201204190339</jacoco.version>     </properties>      <dependencies>         <dependency>             <groupId>junit</groupId>             <artifactId>junit-dep</artifactId>             <version>4.10</version>             <scope>test</scope>             <exclusions>                 <exclusion>                     <groupId>org.hamcrest</groupId>                     <artifactId>hamcrest-core</artifactId>                 </exclusion>             </exclusions>         </dependency>          <dependency>             <groupId>org.hamcrest</groupId>             <artifactId>hamcrest-core</artifactId>             <version>1.3.RC2</version>             <scope>test</scope>         </dependency>          <dependency>             <groupId>org.hamcrest</groupId>             <artifactId>hamcrest-library</artifactId>             <version>1.3.RC2</version>             <scope>test</scope>         </dependency>     </dependencies>      <build>         <plugins>             <plugin>                 <groupId>org.jacoco</groupId>                 <artifactId>jacoco-maven-plugin</artifactId>                 <version>0.7.5.201505241946</version>                 <configuration>                     <destFile>${project.basedir}/../target/jacoco.exec</destFile>                     <append>true</append>                 </configuration>                 <executions>                     <execution>                         <id>jacoco-initialize</id>                         <goals>                             <goal>prepare-agent</goal>                         </goals>                     </execution>                     <execution>                         <id>jacoco-site</id>                         <phase>package</phase>                         <goals>                             <goal>report</goal>                         </goals>                     </execution>                 </executions>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-failsafe-plugin</artifactId>                 <version>2.16</version>                 <executions>                     <execution>                         <id>default-integration-test</id>                         <goals>                             <goal>integration-test</goal>                         </goals>                     </execution>                 </executions>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-surefire-plugin</artifactId>                 <version>2.16</version>             </plugin>         </plugins>     </build>  </project> 

A Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <artifactId>jacoco-multi</artifactId>         <groupId>com.test</groupId>         <version>0.0.1-SNAPSHOT</version>         <relativePath>..</relativePath>     </parent>     <artifactId>projectA</artifactId>      <build>         <pluginManagement>             <plugins>                 <plugin>                     <groupId>org.apache.maven.plugins</groupId>                     <artifactId>maven-surefire-plugin</artifactId>                     <version>2.12</version>                 </plugin>              </plugins>         </pluginManagement>     </build>  </project> 

B pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent>     <artifactId>jacoco-multi</artifactId>     <groupId>com.test</groupId>     <version>0.0.1-SNAPSHOT</version>     <relativePath>..</relativePath> </parent> <artifactId>projectB</artifactId>  <dependencies>     <dependency>         <groupId>com.test</groupId>         <artifactId>projectA</artifactId>         <version>0.0.1-SNAPSHOT</version>     </dependency> </dependencies>  <build>     <pluginManagement>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-surefire-plugin</artifactId>                 <version>2.12</version>             </plugin>         </plugins>     </pluginManagement> </build> 

I am executing this command mvn clean package. I can see jacoco.exec is getting generated, however I am not able to see any HTML reports are being to verify the data.

Please help me with this. Another point is, my configuration is correct for Multi-Module projects?

Update

Identified issue. <destFile>${project.basedir}/../target/jacoco.exec</destFile> changed to <destFile>${project.basedir}/target/jacoco.exec</destFile>

Now it's generating reports for individual modules. Any idea how to generate consolidated report

like image 530
RaceBase Avatar asked Oct 12 '15 10:10

RaceBase


People also ask

How do you use JaCoCo for code coverage in maven?

Run the mvn package command. The package command will invoke the test phase during the packaging of the project jar or war file. In the test phase, JaCoCo agent will initialize and run the code coverage analysis while the tests are executed.

How code coverage is calculated in JaCoCo?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100. 16) Where is the JaCoCo report generated Gradle?

Why do we need JaCoCo with SonarQube?

SonarQube and JaCoCo are two tools that we can use together to make it easy to measure code coverage. They also provide an overview of the overall health of the source code by finding code duplications, bugs and other issues in the code. This helps us to know whether our code is production-ready or not.


1 Answers

JaCoCo version 0.7.7 can generate an aggregate coverage report from multiple Maven modules through a new goal jacoco:report-aggregate.

like image 85
Rogério Avatar answered Sep 18 '22 14:09

Rogério