【问题标题】:maven is not compiling test/java classes into target foldermaven 没有将 test/java 类编译到目标文件夹中
【发布时间】:2017-11-30 15:50:44
【问题描述】:

我尝试通过 maven->clean 和 project->clean 清理我的项目,但没有奏效。我尝试在构建路径下显式更改我的输出文件夹,但不确定为 src/test/java 选择什么

我还在 pom.xml 中更新了我的 testng 插件

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd" >
  <modelVersion>4.0.0</modelVersion>
  <groupId>abc</groupId>
  <artifactId>xyz</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <description>Test</description>
  <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
            <scope>compile</scope>
        </dependency>
    
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>3.3.1</version>
        </dependency>
        
        
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
    
    <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-chrome-driver</artifactId>
         <version>3.3.1</version>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-ie-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-ie-driver</artifactId>
        <version>3.3.1</version>
    </dependency>
        
        <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.16</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.16</version>
    </dependency>
        
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>
    
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
            <!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
        <dependency>
             <groupId>com.relevantcodes</groupId>
             <artifactId>extentreports</artifactId>
            <version>2.41.0</version>
        </dependency>
    
        
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.1.0</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
    
    </dependencies>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
 
  <build>

   <resources>
     <resource>
       <directory>src/test/resources</directory>
       <filtering>false</filtering>
       <includes>
          <include>**/*.properties</include>
          
        </includes>
     </resource>
   </resources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>

                <source>1.8</source>
                <target>1.8</target>
             
            </configuration>
        </plugin>
    </plugins>

  </build>
</project>

src/test/java 和 src/test/resources 的输出文件夹是 /target/classes

我得到 TestNG class not found in classpath 错误:

org.testng.TestNGException: 
Cannot find class in classpath: testclass file

之前这段代码运行良好。我只是尝试通过更改 maven 项目的工件 id 来重构项目,然后无法继续。

【问题讨论】:

  • 将 testng 依赖设置为范围 test 而不是 compile
  • 您尝试过从命令行运行 Maven,还是仅从 Eclipse 运行?

标签: java eclipse maven


【解决方案1】:

不确定这适用的范围有多广,但我在这方面取得了成功:

右键 > Java 构建路径 > 源 确保检查所有 4 个文件夹的 Include:(All) 和 Exclude:(None)

src/main/java

src/main/资源

src/test/java

src/test/resources

【讨论】:

    【解决方案2】:

    您需要添加 ma​​ven surefire 插件 来运行 testNG 类。如果您能够在没有此插件的情况下更早地运行测试,那将是一个奇迹。您必须在构建阶段添加以下插件才能开始。

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
          <includes>
            <include>Sample.java</include>
          </includes>
        </configuration>
      </plugin>
    

    如果您不想手动包含测试,那么默认情况下,Surefire 插件将自动包含所有具有以下通配符模式的测试类:

    "**/Test*.java"
    
    "**/*Test.java"
    
    "**/*Tests.java"
    
    "**/*TestCase.java"
    

    【讨论】:

      【解决方案3】:

      我认为你必须将非 maven 项目转换为 maven 项目

      right click on project->Configure->Convert it to Maven
      

      【讨论】:

      • 这是 maven 项目。
      • 试试这个,只做 Eclipse> Project > Clean 然后再次运行测试用例。它应该工作。它在后台所做的是,它将在您的项目目录中调用 mvn eclipse:clean ,这将删除您的 .project 和 .classpath 文件,您还可以执行 mvn eclipse:eclipse - 这会重新生成您的 .project 和 .classpath 文件。从而在类路径中添加所需的类。
      • 试过了。不为我工作。我什至没有在目标文件夹中看到我编译的测试类
      【解决方案4】:

      对我来说,问题是“跳过测试”复选框被选中。

      RClick 项目 -> 运行方式 -> 运行配置 -> Maven 构建(选择您的构建配置) -> 取消选中跳过测试复选框

      此复选框可防止 Maven 编译测试

      如果您想编译测试,但不想在构建过程中执行它们,请在目标中添加 -DskipTests

      【讨论】:

        猜你喜欢
        • 2016-01-17
        • 2013-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-19
        相关资源
        最近更新 更多