天天看点

7.Eclipse中创建新Maven项目



第一步:首先导入前面命令行建立的两个maven项目hello和hellofriend。

      方法:选择file-->import-->existing maven projects选项选择对应项目路径导入即可

第二步:按顺序先后执行hello和hellofriend项目的的构建

      方法:右击各自项目pom.xml文件,选择run as中的maveninstall命令将构件安装至仓库中

第三步:通过eclipse新建第三个maven项目。选择file-->new-->other-->mavenproject选项

第四步:在src/main/java中新建文件cn.toto.maven.makefriends.java

package cn.toto.maven;

public class makefriends {

   public stringmakefriends(string name){

      hellofriendfriend = new hellofriend();

      friend.sayhellotofriend("litingwei"); 

      string str= "hey,"+friend.getmyname()+" make a friend please.";

      system.out.println(str);

      returnstr;

   }

}

第五步:在src/test/java中新建文件cn.toto.maven.makefriendstest.java

import static junit.framework.assert.assertequals;

import static org.junit.assert.*;

import org.junit.test;

public class makefriendstest {

   @test

   public voidtestmakefriends(){    

      makefriendsmakefriend = new makefriends();

      string str= makefriend.makefriends("tuzuoquan");

      assertequals("hey,johnmake a friend please.",str);    

第六步:点击根目录pom.xml添加依赖

    <dependencies>

      <dependency>

          <groupid>junit</groupid>

          <artifactid>junit</artifactid>

          <version>4.9</version>

          <scope>test</scope>

      </dependency>

    <groupid>cn.toto.maven</groupid>

    <artifactid>hellofriend</artifactid>

    <version>0.0.1-snapshot</version>

    <type>jar</type>

    <scope>compile</scope>

  </dependency>

    </dependencies>

第七步:右击pom.xml选择run as中的命令执行即可

继续阅读