Issue
I'm just going to start this off with the links I have already checked:
Jacoco: IllegalStateException: Can't add different class with same name
jacoco : Cannot exclude classes
JaCoCo test coverage: How to exclude a class inside jar from report?
JaCoCo Can't add different class with same name: org/hamcrest/BaseDescription
These are just the SO questions I have checked, not to mention all the GitHub posts too..
I have spent 2 full days now trying to find the solution for my problem, but I have yet to find anything that works.
So the story goes like this:
- NetBeans project for automation testing
- The company I work for has a jar file
- I used below command to include this jar file in my project:
mvn install:install-file -
Dfile=C:\MavenSelenium\Automation_Framework\src\main\resources\Dependencies\java-api-1.0-jar-with-
dependencies.jar -DgroupId=this.jar.file -DartifactId=api-bundle -Dversion=1.0 -Dpackaging=jar
- Added the dependency to the pom.xml file
<dependency>
<groupId>this.jar.file</groupId>
<artifactId>api-bundle</artifactId>
<version>1.0</version>
</dependency>
- Open CMD, go to the location of the NetBeans project
- Execute the following maven command (Or Clean and Build Project in NetBeans):
mvn clean verify
- Everything goes fine, EXCEPT when JaCoCo tries to generate the report. Below is the error log:
[INFO] --- jacoco-maven-plugin:0.8.6:report (report) @ Automation_Framework ---
[INFO] Loading execution data file C:\MavenSelenium\Automation_Framework\target\jacoco.exec
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.851 s
[INFO] Finished at: 2020-12-20T20:35:00+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.8.6:report (report) on project Automation_Framework:
An error has occurred in JaCoCo report generation.: Error while creating report: Error while analyzing
C:\MavenSelenium\Automation_Framework\target\classes\Dependencies\
java-api-1.0-jar-with-dependencies.jar@org/apache/logging/log4j/core/util/SystemClock.class.
Can't add different class with same name: org/apache/logging/log4j/core/util/SystemClock -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
The JAR file that is mentioned in the error above is the JAR file/dependency from my company
This is my pom.xml file:
<modelVersion>4.0.0</modelVersion>
<groupId>Automation_Framework</groupId>
<artifactId>Automation_Framework</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>
<!-- Selenium Java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- Selenium API -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.141.59</version>
</dependency>
<!-- Selenium Remote Driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.141.59</version>
</dependency>
<!-- Selenium Chrome Driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
</dependency>
<!-- Selenium Common -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-common</artifactId>
<version>2.0b1</version>
</dependency>
<!-- Apache Commons Lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<!-- Video Recorder -->
<dependency>
<groupId>com.automation-remarks</groupId>
<artifactId>video-recorder</artifactId>
<version>1.0</version>
</dependency>
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- Apache POI OOXML-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!-- JCraft -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<!-- Extent - RX Java -->
<dependency>
<groupId>io.reactivex.rxjava3</groupId>
<artifactId>rxjava</artifactId>
<version>3.0.4</version>
</dependency>
<!-- Extent - Free Marker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<!-- Extent - Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- Extent - GSON -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<!-- Apache JMeter Core -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>5.4</version>
</dependency>
<!-- Apache JMeter Components -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_components</artifactId>
<version>5.4</version>
</dependency>
<!-- Apache JMeter JOrphan -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>jorphan</artifactId>
<version>5.4</version>
</dependency>
<!-- Apache JMeter JUnit -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_junit</artifactId>
<version>5.4</version>
</dependency>
<!-- Apache JMeter HTTP -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_http</artifactId>
<version>5.4</version>
</dependency>
<!-- Apache HTTP Core -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.14</version>
</dependency>
<!-- XStream Core -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.15</version>
</dependency>
<!-- Aerogear OTP -->
<dependency>
<groupId>org.jboss.aerogear</groupId>
<artifactId>aerogear-otp-java</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Appium Java Client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.4.1</version>
</dependency>
<!-- Company Jar (API) -->
<dependency>
<groupId>this.jar.file</groupId>
<artifactId>api-bundle</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>this.jar.file</groupId>
<artifactId>api-bundle</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Surefire Plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</dependency>
</dependencies>
<!-- Extent - Maven Checkstyle Plugin -->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.16</version>
<reportSets>
<reportSet>
<reports>
<report>checkstyle</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<configuration>
<excludes>
<exclude>**/util/SystemClock.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<name>Automation_Framework</name>
</project>
You'll notice I have an exclusion by the actual dependency jar for the company I work for:
<!-- Company Jar (API) -->
<dependency>
<groupId>this.jar.file</groupId>
<artifactId>api-bundle</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>this.jar.file</groupId>
<artifactId>api-bundle</artifactId>
</exclusion>
</exclusions>
</dependency>
as well as by the JaCoCo plugin section
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<configuration>
<excludes>
<exclude>**/util/SystemClock.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
neither works (at least that I can tell).
I honestly don't understand the half of the "technical talk" that I read and I am sure with more reading throughout my time of investigating all of this, I will begin to understand it BUT I cannot wait until I magically figure it out, so I am hoping someone can assist.
It's the first time I have setup a project like this, so admittedly there's plenty for me to still learn.
Solution
Okay, so I believe I found the cause for my issue..
As far as I can tell, it was due to the way in which I added/stored the external JAR file for my company.
The JAR file was in my project folder:
C:\MavenSelenium\Automation_Framework\src\main\resources\Dependencies\java-api-1.0-jar-with-dependencies.jar
I removed the file from this location AND I deleted it from my list of actual dependencies.
I then tried to use the initial command from before, but with the file now in my downloads folder:
mvn install:install-file -
Dfile=C:\Users\{username}\Downloads\java-api-1.0-jar-with-
dependencies.jar -DgroupId=this.jar.file -DartifactId=api-bundle -Dversion=1.0 -
Dpackaging=jar
However, I did not see it appear under my dependencies.
THEN, I right clicked on my dependencies folder (the one that is defaultly created with the maven project) and I clicked "Add Dependency" as below:
After I did this, I saw the dependency added under dependencies AND I saw the pom.xml updated with the addition of this dependency as well.
Code added to pom.xml automatically:
<!-- API -->
<dependency>
<groupId>this.jar.file</groupId>
<artifactId>api-bundle</artifactId>
<version>1.0</version>
</dependency>
Answered By - Eitel Dagnin
Answer Checked By - Timothy Miller (JavaFixing Admin)