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

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

我将从我已经检查过的链接开始:

Jacoco: IllegalStateException: Can't add different class with same name

JaCoCo test coverage: How to exclude a class inside jar from report?

JaCoCo Can't add different class with same name: org/hamcrest/BaseDescription

这些只是我检查过的 SO 问题,更不用说所有 GitHub 帖子了..

我已经花了整整 2 天时间来寻找 我的 问题的解决方案,但我还没有找到任何有效的方法。

所以故事是这样的:

  1. 用于自动化测试的 NetBeans 项目
  2. 我工作的公司有一个 jar 文件
  3. 我使用以下命令将此 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. 将依赖项添加到 pom.xml 文件
    <dependency>
        <groupId>this.jar.file</groupId>
        <artifactId>api-bundle</artifactId>
        <version>1.0</version>
    </dependency>
  1. 打开CMD,转到NetBeans项目所在位置
  2. 执行以下 Maven 命令(或在 NetBeans 中清理并构建项目):
    mvn clean verify
  1. 一切顺利,除了 JaCoCo 尝试生成报告。以下是错误日志:
[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. 上面错误中提到的JAR文件是我公司的JARfile/dependency

  2. 这是我的 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>

你会注意到我工作的公司的实际依赖 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>

以及 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>

两者都不起作用(至少我可以这么说)。

老实说,我读了一半的“技术谈话”我都不明白,我相信在我调查所有这些的过程中阅读更多的书,我会开始理解它,但我不能等到我神奇地弄明白了,所以我希望有人能提供帮助。

这是我第一次设置这样的项目,所以诚然,我还有很多东西需要学习。

好的,我相信我找到了问题的原因..

据我所知,这是因为我 added/stored 我公司的外部 JAR 文件的方式。

JAR 文件在我的项目文件夹中:

C:\MavenSelenium\Automation_Framework\src\main\resources\Dependencies\java-api-1.0-jar-with-dependencies.jar

我从该位置删除了该文件,并将其从我的实际 依赖项列表中删除。

然后我尝试使用之前的初始命令,但文件现在在我的下载文件夹中:

    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

但是,我没有看到它出现在我的依赖项下。

然后,我右键单击我的依赖项文件夹(默认使用 maven 项目创建的那个),然后单击“添加依赖项”,如下所示:

执行此操作后,我看到在 dependencies 下添加了依赖项并且我看到 pom.xml 也随着添加此依赖项而更新。

自动添加到 pom.xml 的代码:

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