【问题标题】:How to add multiple source directories to pom.xml如何将多个源目录添加到 pom.xml
【发布时间】:2013-05-10 18:36:54
【问题描述】:

当我尝试使用 maven 编译多个源目录时,我收到包 org.testng.annotations 不存在错误。我正在运行 webdriver 测试,我所有的测试都在导入 import org.testng.annotations。我正在使用 IntelliJ 12,我的 src 目录看起来像这样 -

src

  -->main
     --> java
        --> package1
            --> file1.java
        --> package2
            --> file2.java
  -->test
     --> java
        --> package1
           --> file1.java
           --> file2.java
        --> package2
        --> package3
        --> package4
        --> package5
        --> package6

我在 pom.xml 中使用的构建插件看起来像这样 -

<?xml version="1.0" encoding="UTF-8"?>
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">

<parent>
    <artifactId>core-xxxxx</artifactId>
    <groupId>core-xxxxx</groupId>
    <version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>tests</artifactId>
   <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>2.32.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
    <build>
       <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
 <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>

                        <sources>
                            <source>src/main/java</source>
                            <source>src/test/java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
</plugins>
</build>
</project>

为什么我收到 org.testng.annotations not found 错误?

【问题讨论】:

标签: selenium maven-2 webdriver testng


【解决方案1】:

maven 项目的默认布局如下:

src
   main
      java
   test
      java

src/main/java/ 目录中,应该添加用于生产 Java 类源文件的包名称。在 src/test/java/ 中是单元测试 java 类源文件的包名。

如果您没有非常好的理由这样做,您应该更改布局。

此外,重新定义 Maven 默认值(目标、目标/类、目标/测试类等)的默认值是违反配置范例的约定。

请注意,您需要遵循命名 convention for unit tests,这意味着您的单元测试应命名如下:

<includes>
 <include>**/*Test*.java</include>
 <include>**/*Test.java</include>
 <include>**/*TestCase.java</include>
</includes>

但在您的情况下,我假设我们正在谈论集成测试,这意味着您需要使用以下naming convention

<includes>
 <include>**/IT*.java</include>
 <include>**/*IT.java</include>
 <include>**/*ITCase.java</include>
</includes>

除此之外,您现在应该知道 Maven 中的单元测试将由 maven-surefire-plugin 执行,而集成测试将由 maven-failsafe-plugin 执行。

【讨论】:

  • @khmarbaise--感谢您的意见。上面的方法为我做了一个干净的编译(mvn clean compile)但是当我用这个命令运行测试时 mvn clean test -DsuiteFile=resources/test-suites/audioSuite.xml 它会抛出错误包2不存在这是一个包在第一个源目录中。有什么想法吗?
【解决方案2】:

为了绕过 testng annotations not found 错误,我最终将基类从 src/main/java/package1/baseclass.java 移动到 src/main/test/package1/baseclass.java 并解决了 testng annotations 错误

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2019-12-25
    • 2012-08-02
    • 2012-07-04
    • 2019-07-03
    • 2019-09-23
    相关资源
    最近更新 更多