Selenium 如何将多个源目录添加到pom.xml

Selenium 如何将多个源目录添加到pom.xml,selenium,maven-2,webdriver,testng,Selenium,Maven 2,Webdriver,Testng,当我试图使用maven编译多个源目录时,我得到的包org.testng.annotations不存在错误。我正在运行webdriver测试,所有测试都在导入import org.testng.annotations。我正在使用Intellij12,我的src目录如下所示- <?xml version="1.0" encoding="UTF-8"?> project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht

当我试图使用maven编译多个源目录时,我得到的包org.testng.annotations不存在错误。我正在运行webdriver测试,所有测试都在导入import org.testng.annotations。我正在使用Intellij12,我的src目录如下所示-

<?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>
src

我在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>

项目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">
核心-xxxxx
核心-xxxxx
1
4.0.0
测验
org.seleniumhq.selenium
硒原料药
2.32.0
org.testng
testng
6.8.1
测试
目标
目标/类别
目标/测试类
org.codehaus.mojo
构建助手maven插件
添加源
生成源
添加源
src/main/java
src/test/java

为什么会出现org.testng.annotations not found错误?

maven项目的默认布局如下:

src
   main
      java
   test
      java
<includes>
 <include>**/*Test*.java</include>
 <include>**/*Test.java</include>
 <include>**/*TestCase.java</include>
</includes>
在src/main/java/目录中,应该添加生产性java类源文件的包名。在src/test/java/中,单元测试java类源文件的包名

如果你没有很好的理由,你不应该改变布局

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

请注意,您需要遵循一个命名,这意味着您的单元测试应该按照以下方式命名:

src
   main
      java
   test
      java
<includes>
 <include>**/*Test*.java</include>
 <include>**/*Test.java</include>
 <include>**/*TestCase.java</include>
</includes>

**/*Test*.java
**/*Test.java
**/*TestCase.java
但是在您的例子中,我假设我们讨论的是集成测试,这意味着您需要使用以下内容


**/IT*.java
**/*IT.java
**/*ITCase.java

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

为了避免testng注释未发现错误,我最终将基类从src/main/java/package1/baseclass.java移动到src/main/test/package1/baseclass.java和testng注释错误已解决

我确实查看了这里,但这并不能解决我的问题-您可以发布完整的pom文件和项目的完整目录结构吗?@khmarbaise-谢谢您的输入。上面的方法为我执行干净编译(mvn clean compile),但是当我使用这个命令mvn clean test-DsuiteFile=resources/test suites/audioSuite.xml运行测试时,它会抛出错误,即包2不存在,它是第一个源目录中的包。有什么想法吗?