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

时间:2013-05-16 01:49:03

标签: selenium maven-2 webdriver testng

当我尝试使用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错误?

2 个答案:

答案 0 :(得分:1)

maven项目的默认布局如下:

src
   main
      java
   test
      java

src / main / java / 目录中,应添加生产性java类源文件的包名。在 src / test / java / 中,单元的包名测试java类源文件。

如果没有真正合理的理由,你应该 NOT 更改布局。

此外,重新定义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-surefire-plugin执行Maven中的单元测试,而maven-failsafe-plugin执行集成测试。

答案 1 :(得分:0)

为了解决testng注释未找到错误,我最终将基类从src / main / java / package1 / baseclass.java移动到src / main / test / package1 / baseclass.java并解决了testng注释错误< / p>

相关问题