一. 创建Java项目
第1步:首先导入前面命令行建立的两个maven项目Hello和HelloFriend。
方法:选择file-->import-->Existing MAVEN PROJECTS选项选择对应项目路径导入即可
第2步:按顺序先后执行Hello和HelloFriend项目的的构建
方法:右击各自项目pom.xml文件,选择run as中的maven install命令将构件安装至仓库中
第3步:通过eclipse新建第三个maven项目。选择file-->new-->other-->MAVEN PROJECT选项

第4步:在src/main/java中新建文件com.zdp.maven.MakeFriends.java
public class MakeFriends {
public String makeFriends(String name){
HelloFriend friend = new HelloFriend();
friend.sayHelloToFriend("litingwei");
String str = "Hey,"+friend.getMyName()+" make a friend please.";
System.out.println(str);
return str;
}
}
第5步:在src/test/java中新建文件com.zdp.maven.MakeFriendsTest.java
public class MakeFriendsTest {
@Test
public void testMakeFriends(){
MakeFriends makeFriend = new MakeFriends();
String str = makeFriend.makeFriends("litingwei");
assertEquals("Hey,John make a friend please.",str);
}
}
第6步:点击根目录pom.xml添加依赖
4.0.0
com.zdp.maven
MakeFriends
0.0.1-SNAPSHOT
jar
MakeFriends
http://maven.apache.org
UTF-8
junit
junit
4.9
test
com.zdp.maven
HelloFriend
0.0.1-SNAPSHOT
compile
第7步:右击pom.xml选择run as 中的命令执行即可
二. 创建Web项目
第1步:创建maven web工程

第2步: 继承parent, 修改web项目中 pom.xml文件如下
4.0.0
webs
war
webs Maven Webapp
com.zdp.maven
Parent
0.0.1-SNAPSHOT
../Parent/pom.xml
junit
junit
com.zdp.maven
MakeFriends
第3步:建立测试jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.zdp.maven.*"%>
<%
MakeFriends makeFriends=new MakeFriends();
out.println(makeFriends.makeFriends("wanglipeng"));
%>
第4步:模块聚合, 修改parent中 pom.xml
4.0.0
com.zdp.maven
Parent
0.0.1-SNAPSHOT
pom
Parent
http://maven.apache.org
UTF-8
../Hello
../HelloFriend
../MakeFriends
../webs
junit
junit
4.9
test
com.zdp.maven
Hello
0.0.1-SNAPSHOT
compile
com.zdp.maven
HelloFriend
0.0.1-SNAPSHOT
compile
com.zdp.maven
MakeFriends
0.0.1-SNAPSHOT
compile
第5步:修改web项目, 设置自动部署到tomcat下面
web
org.codehaus.cargo
cargo-maven2-plugin
1.2.3
tomcat5x
D:/tomcats/apache-tomcat-6.0.32_Demo
existing
D:/tomcats/apache-tomcat-6.0.32_Demo
cargo-run
install
run