http://my.oschina.net/810965872/blog/148634
myeclipse中需要集成maven3,具体操作可以参看这篇文章
http://www.cnblogs.com/fancyzero/archive/2012/06/09/maven3.html
这里就不多加描述
在创建web工程前,咱们先创建一个普通的maven工程,后面的web工程将会使用到这个工程的jar包
1.普通maven工程创建过程如下,右键>>new Maven Project>>next
输入对应的groupId,artifactId及包名
生成的文件目录结构如下
修改App.java
1
2
3
4
5
6
7
8
9
10
11
12
|
package
com.johnmy.study;
/**
*
* @author JohnmyWork
* @date 2013-7-30
*/
public
class
App {
public
static
String handleString(String str) {
return
"handled:"
+ str;
}
}
|
此时会在本地库中安装mvnapp的资源内容,下面的web工程可以直接从maven库中读取到资源
其实,还有一种更高效的方法,可以在dos窗口中使用mvn命令快速创建普通工程,再导入到myeclipse中编辑
-----------------------------------------------------------------------------------------------------------------
现在开始创建web工程
同样new maven project >> next
输入相应信息
生成的目录结构如下
修改目录结构
修改之后如下
向工程中添加资源包
也可以直接修改pom文件,这里引入了之前创建的普通工程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<
dependencies
>
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>3.8.1</
version
>
<
scope
>test</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>javax.servlet</
groupId
>
<
artifactId
>servlet-api</
artifactId
>
<
version
>2.4</
version
>
</
dependency
>
<
dependency
>
<
groupId
>com.johnmy.study</
groupId
>
<
artifactId
>mvnapp</
artifactId
>
<
version
>1.0-SNAPSHOT</
version
>
</
dependency
>
</
dependencies
>
|
新建Myservlet.java并放到study包中
Myservlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package
study;
import
java.io.IOException;
import
java.io.Writer;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
com.johnmy.study.App;
/**
*
* @author JohnmyWork
* @date 2013-7-31
*/
public
class
Myservlet
extends
HttpServlet {
@Override
protected
void
doPost(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException {
super
.doGet(req, resp);
}
@Override
protected
void
doGet(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException {
String input = req.getParameter(
"name"
);
Writer out = resp.getWriter();
out.append(App.handleString(input));
}
private
static
final
long
serialVersionUID = 1L;
}
|
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!
DOCTYPE
web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<
web-app
>
<
display-name
>Archetype Created Web Application</
display-name
>
<
welcome-file-list
>
<
welcome-file
>index.jsp</
welcome-file
>
</
welcome-file-list
>
<
servlet
>
<
servlet-name
>Myservlet</
servlet-name
>
<
servlet-class
>study.Myservlet</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>Myservlet</
servlet-name
>
<
url-pattern
>/myservlet</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
|
index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
|
<!
doctype
html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
/>
<
title
>hello maven!</
title
>
</
head
>
<
body
>
<
form
action
=
"myservlet"
method
=
"get"
>
name:<
input
name
=
"name"
> <
input
type
=
"submit"
>
</
form
>
</
body
>
</
html
>
|
BUILD SUCCESS !!一切正常
下面就把项目部署到tomcat7中
部署之前先修改下tomcat的配置文件
D:\WebContainer\3-tomcat7\apache-tomcat-7.0.39\conf\tomcat-users.xml
1
2
3
4
5
6
7
|
<?
xml
version
=
'1.0'
encoding
=
'utf-8'
?>
<
tomcat-users
>
<
role
rolename
=
"manager-gui"
/>
<
role
rolename
=
"manager"
/>
<
role
rolename
=
"manager-script"
/>
<
user
username
=
"tomcat"
password
=
"tomcat"
roles
=
"manager-gui,manager,manager-script"
/>
</
tomcat-users
>
|
D:\Program Files\MavenServer\maven3\conf\settings.xml
找到servers修改之
1
2
3
4
5
6
7
|
<
servers
>
<
server
>
<
id
>mytomcat7</
id
>
<
username
>tomcat</
username
>
<
password
>tomcat</
password
>
</
server
>
</
servers
>
|
外部配置好了,现在配置下web工程中的pom,添加tomcat7-maven-plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<
build
>
<
finalName
>mvnwebapp</
finalName
>
<
plugins
>
<
plugin
>
<
groupId
>org.apache.tomcat.maven</
groupId
>
<
artifactId
>tomcat7-maven-plugin</
artifactId
>
<
version
>2.2-SNAPSHOT</
version
>
<
configuration
>
<
path
>/${project.build.finalName}</
path
>
<
server
>mytomcat7</
server
>
<!-- 这里是本地tomcat,如果是远程服务器可以改成对应的地址,实现自动部署-->
<
url
>http://localhost:8080/manager/text</
url
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
|
添加插件仓库配置,下面这两个任选其一或者两个都用
1
2
3
4
5
6
7
8
9
10
11
12
|
<
repositories
>
<
repository
>
<
id
>apache.snapshots</
id
>
<
url
>https://repository.apache.org/content/repositories/snapshots</
url
>
<
releases
>
<
enabled
>false</
enabled
>
</
releases
>
<
snapshots
>
<
enabled
>true</
enabled
>
</
snapshots
>
</
repository
>
</
repositories
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<
pluginRepositories
>
<
pluginRepository
>
<
id
>apache.snapshots</
id
>
<
name
>Apache Snapshots</
name
>
<
url
>https://repository.apache.org/content/repositories/snapshots</
url
>
<
releases
>
<
enabled
>false</
enabled
>
</
releases
>
<
snapshots
>
<
enabled
>true</
enabled
>
</
snapshots
>
</
pluginRepository
>
</
pluginRepositories
>
|
启动tomcat7,执行maven build
配置goals
run
访问http://localhost:8080/mvnwebapp/
提交后
成功!!!!!!
调用了普通工程的jar中的handleString()方法
如果普通工程的jar有更新并重新发布到tomcat中
只需要简单的maven install,maven bulid
修改handleString方法
1
2
3
|
public
static
String handleString(String str) {
return
"nice!!! you handled it:"
+ str+
"\n"
+
new
Date().toString();
}
|
在mvnapp中执行maven install
在mvnwebapp中执行maven bulid
--------------------------------完---------------------------------
如有错误或问题,请指出,谢谢