简体   繁体   English

NetBeans - Maven - JaCoCo - 无法添加具有相同名称的不同 class

[英]NetBeans - Maven - JaCoCo - Can't add different class with same name

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:IllegalStateException:无法添加具有相同名称的不同 class

jacoco: Cannot exclude classes jacoco:不能排除类

JaCoCo test coverage: How to exclude a class inside jar from report? JaCoCo 测试覆盖率:如何从报告中排除 jar 内的 class?

JaCoCo Can't add different class with same name: org/hamcrest/BaseDescription JaCoCo 无法添加具有相同名称的不同 class:org/hamcrest/BaseDescription

These are just the SO questions I have checked, not to mention all the GitHub posts too..这些只是我检查过的 SO 问题,更不用说所有 GitHub 帖子了。

I have spent 2 full days now trying to find the solution for my problem, but I have yet to find anything that works.我已经花了整整 2 天的时间试图找到解决问题的方法,但还没有找到任何可行的方法。

So the story goes like this:所以故事是这样的:

  1. NetBeans project for automation testing NetBeans 项目用于自动化测试
  2. The company I work for has a jar file我工作的公司有一个 jar 文件
  3. I used below command to include this jar file in my project:我使用以下命令在我的项目中包含此 jar 文件:
    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
  1. Added the dependency to the pom.xml filepom.xml文件中添加了依赖项
    <dependency>
        <groupId>this.jar.file</groupId>
        <artifactId>api-bundle</artifactId>
        <version>1.0</version>
    </dependency>
  1. Open CMD, go to the location of the NetBeans project打开CMD、go到NetBeans项目的位置
  2. Execute the following maven command (Or Clean and Build Project in NetBeans):执行以下 maven 命令(或在 NetBeans 中清理并构建项目):
    mvn clean verify
  1. Everything goes fine, EXCEPT when JaCoCo tries to generate the report.一切都很好,除了 JaCoCo 尝试生成报告时。 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
  1. The JAR file that is mentioned in the error above is the JAR file/dependency from my company上面错误中提到的 JAR 文件是我公司的 JAR 文件/依赖项

  2. This is my pom.xml file:这是我的 pom.xml 文件:

        <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:您会注意到我为我工作的公司的实际依赖项 jar 排除了:

<!-- 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以及 JaCoCo 插件部分

     <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.这是我第一次设置这样的项目,所以我承认我还有很多东西要学习。

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.据我所知,这是由于我为我的公司添加/存储外部 JAR 文件的方式。

The JAR file was in my project folder: JAR 文件在我的项目文件夹中:

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:然后,我右键单击我的依赖项文件夹(默认使用 maven 项目创建的文件夹),然后单击“添加依赖项”,如下所示:

在此处输入图像描述

在此处输入图像描述

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.完成此操作后,我看到在依赖项下添加了依赖项,并且我看到 pom.xml 也随着添加此依赖项而更新。

Code added to pom.xml automatically:自动添加到 pom.xml 的代码:

        <!-- API -->
        <dependency>
            <groupId>this.jar.file</groupId>
            <artifactId>api-bundle</artifactId>
            <version>1.0</version>
        </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM