如何在Maven中导入其他项目

时间:2015-11-18 16:19:15

标签: java maven testng

我有两个项目com.mvnworkstation.commoncom.mvnworkstation.test我在项目公共中创建了基类,在项目测试中创建了所有测试。

我为项目com.mvnworkstation.common

创建了如下所示的pom
<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>com.mvnworkstation.common</groupId>
  <artifactId>com.mvnworkstation.common</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

 <dependencies>
    <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.1.1</version>
    <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.48.2</version>
    </dependency>  
</dependencies>

<build>
   <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
         <source>1.7</source>
         <target>1.7</target>
         </configuration>
     </plugin>  
     </plugins>
 </build>
</project>

并为项目com.mvnworkstation.test

创建了如下所示的pom
<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>com.mvnworkstation.test</groupId>
  <artifactId>com.mvnworkstation.test</artifactId>
  <version>0.0.1-SNAPSHOTA</version>


<dependencies>
 <dependency>
   <groupId>com.mvnworkstation.common</groupId>
   <artifactId>com.mvnworkstation.common</artifactId>
   <version>0.0.1-SNAPSHOT</version>
 </dependency>

</dependencies>


<build>
 <sourceDirectory>src</sourceDirectory>
   <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.14.1</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>/suite/Login.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
     </plugin>

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



</plugins>
</build>


</project>
项目Baseclass

中的

和我的com.mvnworkstation.common

package com.mvnworkstation.common.base;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;

public class Baseclass {
    public WebDriver driver = null;
    @BeforeClass
    public void setup(){
          driver = new FirefoxDriver();
          driver.manage().window().maximize();
    }

    @AfterClass
    public void teardown(){
          driver.close();
    }
}

项目com.mvnworkstation.test

中的测试类
package com.mvnworkstation.test.sourcecode;

import org.testng.annotations.Test;

import com.mvnworkstation.common.base.Baseclass;

public class Login extends Baseclass{

    @Test
    public void login(){
        driver.get("https://www.google.co.in");
    }
}

如果我运行我的测试,我将收到编译错误,如下所示,

RROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[3,30] package org.testng.annotations does not exist
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[5,38] package com.mvnworkstation.common.base does not exist
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[7,28] cannot find symbol
  symbol: class Baseclass
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[9,10] cannot find symbol
  symbol:   class Test
  location: class com.mvnworkstation.test.sourcecode.Login
[ERROR] /C:/Users/mona/pomtest/com.mvnworkstation.test/src/com/mvnworkstation/test/sourcecode/Login.java:[11,17] cannot find symbol

我添加了必要的依赖项目来导入基类和testng jar不知道为什么我要编译?有人可以纠正我吗?

1 个答案:

答案 0 :(得分:0)

测试范围依赖性不可传递。

您的项目com.mvnworkstation.test未声明对org.testng:testng的依赖。