Issue
I'm having trouble getting JaCoCo to work with Maven. I keep running into either
Skipping JaCoCo execution due to missing execution data file.
Or
The parameters 'rules' for goal org.jacoco:jacoco-maven-plugin:0.8.2:check are missing or invalid
I also can't seem to get JaCoCo to run with just mvn clean test
instead I have to run mvn clean test jacoco:report
or mvn clean test jacoco:check
I've tried a variety of methods of editing my POM file, such as adding configuration for destFile and dataFile, as well as the POM settings here: https://howtodoinjava.com/junit5/jacoco-test-coverage/ and here https://www.lambdatest.com/blog/reporting-code-coverage-using-maven-and-jacoco-plugin/ . Any help would be greatly appreciated. Below is my POM file
<?xml version="1.0" encoding="UTF-8"?>
<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.NAME.PROJECTNAME</groupId>
<artifactId>PROJECTNAME</artifactId>
<version>1.0-SNAPSHOT</version>
<name>PROJECTNAME</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>HelloFX</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Solution
This is because you are using <pluginManagement>
tag, and <plugins>
are put inside it.
As per the Maven documentation on <pluginManagement>,
pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children or in the current POM. The children have every right to override pluginManagement definitions.
In short, you will use <pluginManagement>
in case of multi-module Maven project. Here is a bit more explanation in this answer.
I removed the <pluginManagement>
tag from pom.xml
and now the build is working and Jacoco report is getting generated.
-> mvn clean verify
...
[INFO] --- jacoco-maven-plugin:0.8.2:prepare-agent (default) @ PROJECTNAME ---
[WARNING] The artifact xml-apis:xml-apis:jar:2.0.2 has been relocated to xml-apis:xml-apis:jar:1.0.b2
[INFO] argLine set to -javaagent:/home/codejournal/.m2/repository/org/jacoco/org.jacoco.agent/0.8.2/org.jacoco.agent-0.8.2-runtime.jar=destfile=/tmp/maven-jacoco/target/coverage-reports/jacoco-unit.exec
...
...
[INFO] --- jacoco-maven-plugin:0.8.2:report (report) @ PROJECTNAME ---
[INFO] Loading execution data file /tmp/maven-jacoco/target/coverage-reports/jacoco-unit.exec
[INFO] Analyzed bundle 'PROJECTNAME' with 1 classes
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ PROJECTNAME ---
[INFO] Building jar: /tmp/maven-jacoco/target/PROJECTNAME-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.141 s
[INFO] Finished at: 2022-04-24T02:18:30+05:30
[INFO] ------------------------------------------------------------------------
-> cat target/site/jacoco/jacoco.csv
GROUP,PACKAGE,CLASS,INSTRUCTION_MISSED,INSTRUCTION_COVERED,BRANCH_MISSED,BRANCH_COVERED,LINE_MISSED,LINE_COVERED,COMPLEXITY_MISSED,COMPLEXITY_COVERED,METHOD_MISSED,METHOD_COVERED
PROJECTNAME,io.codejournal.maven.jacoco,Hello,7,0,0,0,3,0,2,0,2,0
Answered By - Code Journal
Answer Checked By - Marie Seifert (JavaFixing Admin)