You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行mvn test命令报依赖jar包不存在错误,Eclipse可正常运行测试

问题描述

我有两个项目:AutomationFramework是封装了公共库、通用页面、基类等的项目,我将其打包为jar包后添加到主测试项目(自动化测试套件)中。在Eclipse 2021-06版本中可以成功运行全部7个测试用例。我想要通过命令行运行测试,后续再配置到Jenkins中执行,但输入mvn test时出现如下错误:

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/SelectAccrditedFarmOrganizationPage.java:[6,54] package com.agricorp.AutomationResourcesFramework.Base does not exist
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/SelectAccrditedFarmOrganizationPage.java:[7,57] package com.agricorp.AutomationResourcesFramework.Globals does not exist
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/SelectAccrditedFarmOrganizationPage.java:[8,57] package com.agricorp.AutomationResourcesFramework.Helpers does not exist
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/SelectAccrditedFarmOrganizationPage.java:[9,57] package com.agricorp.AutomationResourcesFramework.Helpers does not exist
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/SelectAccrditedFarmOrganizationPage.java:[11,58] cannot find symbol
  symbol: class Base
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/ReviewFarmBusinessInformationPage.java:[10,54] package com.agricorp.AutomationResourcesFramework.Base does not exist
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/ReviewFarmBusinessInformationPage.java:[11,57] package com.agricorp.AutomationResourcesFramework.Globals does not exist
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/ReviewFarmBusinessInformationPage.java:[12,57] package com.agricorp.AutomationResourcesFramework.Helpers does not exist
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/ReviewFarmBusinessInformationPage.java:[13,57] package com.agricorp.AutomationResourcesFramework.Helpers does not exist
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/ReviewFarmBusinessInformationPage.java:[14,57] package com.agricorp.AutomationResourcesFramework.Helpers does not exist
[ERROR] /C:/TFS/TS JAVA Automation/Development/FBRAutomationTestSuite/src/test/java/com/agricorp/FBRAutomationTestSuite/Pages/ReviewFarmBusinessInformationPage.java:[16,56] cannot find symbol
  symbol: class BasePageSetup

错误信息
另外,我在命令行执行mvn clean和mvn compile都可以成功完成,我还注意到上述报错是在查找.java源文件,执行mvn verify也会触发相同错误。
Eclipse中的项目配置如下:
Eclipse项目结构
AutomationResourcesFramework项目pom.xml内容如下:

<name>AutomationResourcesFramework</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.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
  <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
  </dependency> 
  <dependency>
      <groupId>io.github.bonigarcia</groupId>
      <artifactId>webdrivermanager</artifactId>
      <version>3.8.1</version>
  </dependency>
  <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.14.1</version>
  </dependency>
  <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.14.1</version>
  </dependency>
  <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.4.0</version>
      <!--scope>test</scope-->
  </dependency>
  <dependency>
      <groupId>com.aventstack</groupId>
      <artifactId>extentreports</artifactId>
      <version>5.0.8</version>
  </dependency>           
  <dependency>
      <groupId>com.codoid.products</groupId>
      <artifactId>fillo</artifactId>
      <version>1.21</version>
  </dependency>
</dependencies>

  <profiles>
      <profile>
          <id>SanityTests</id>
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <version>3.0.0-M5</version>
                      <configuration>
                          <suiteXmlFiles>
                              <suiteXmlFile>testng.xml</suiteXmlFile>
                          </suiteXmlFiles>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </profile>
      <profile>
          <id>RegressionTests</id>
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <version>3.0.0-M5</version>
                      <configuration>
                          <suiteXmlFiles>
                              <suiteXmlFile>FBRAutomationTestSuite.xml</suiteXmlFile>
                          </suiteXmlFiles>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </profile>
  </profiles>

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.1.0</version>
      </plugin>
      <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-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>
      <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>
    </plugins>
  </pluginManagement>
</build>
</project>

问题根因

Eclipse能正常运行是因为你手动把框架jar包添加到了Eclipse的项目构建路径,但Maven的执行classpath并没有引入该依赖。你执行mvn compile成功是因为该阶段只会编译src/main/java下的代码,你的主测试项目main目录下没有引用框架包的代码,所以不会报错;而test/verify阶段会额外编译src/test/java下的测试代码,这些代码引用了框架包,Maven找不到对应依赖就会触发类找不到的错误。

解决方案

按以下步骤操作即可解决:

  1. 先把公共框架jar安装到本地Maven仓库
    进入AutomationResourcesFramework项目根目录,执行命令:
    mvn clean install
    执行成功后,这个jar就会被放到你本地的maven仓库中,可被其他项目引用。
  2. 在主测试项目FBRAutomationTestSuite的pom.xml中添加依赖
    在主测试项目的dependencies节点下加入以下配置,对应值替换成你框架项目pom里的实际配置即可:
<dependency>
    <groupId>com.agricorp</groupId>
    <artifactId>AutomationResourcesFramework</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
  1. 可选:直接引用本地jar的配置
    如果不想走install流程,也可以直接在主测试项目pom里引入本地jar,注意把路径改成你实际存放jar的路径:
<dependency>
    <groupId>com.agricorp</groupId>
    <artifactId>AutomationResourcesFramework</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/AutomationResourcesFramework.jar</systemPath>
</dependency>
  1. 执行测试验证
    配置完成后回到主测试项目根目录,执行mvn test即可正常运行测试用例,后续配置Jenkins时,提前在Jenkins所在服务器的Maven仓库安装好框架包,就可以正常执行。

内容的提问来源于stack exchange,提问作者user10829672

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 04:00:04