netbeans-maven-jacoco-不能添加同名的不同类

nlejzf6q  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(489)

我将从我已经检查过的链接开始:
jacoco:illegalstateexception:无法添加同名的不同类
jacoco:不能排除类
jacoco测试覆盖率:如何从报告中排除jar中的类?
jacoco无法添加同名的不同类:org/hamcrest/basedescription
这些都是我查过的问题,更不用说所有的github帖子了。。
我已经花了整整两天的时间试图找到解决我问题的方法,但是我还没有找到任何有效的方法。
故事是这样的:
netbeans自动化测试项目
我工作的公司有一个jar文件
我使用以下命令将此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

将依赖项添加到pom.xml文件

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

打开cmd,转到netbeans项目的位置
执行以下maven命令(或在netbeans中清理并生成项目):

mvn clean verify

一切都很顺利,除了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

上面的错误中提到的jar文件是来自我公司的jar文件/依赖关系
这是我的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>

两者都不起作用(至少我能看出来)。
我真的不明白我读到的“技术谈话”的一半,我相信在我调查这一切的过程中,随着更多的阅读,我会开始理解它,但我不能等到我神奇地发现它,所以我希望有人能帮助我。
这是我第一次设置这样的项目,所以无可否认我还有很多东西要学。

ipakzgxi

ipakzgxi1#

好吧,我想我找到了问题的原因。。
据我所知,这是由于我为公司添加/存储外部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

但是,我没有看到它出现在我的依赖项下。
然后,我右键单击我的dependencies文件夹(maven项目默认创建的文件夹),然后单击“add dependency”,如下所示:


完成此操作后,我看到在dependencies下添加了依赖项,并且看到pom.xml也随着这个依赖项的添加而更新。
自动添加到pom.xml的代码:

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

相关问题