Maven实现项目构建直接部署Web项目到Tomcat配置如下:
1.Tomcat的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户。
<
role
rolename
=
"admin-gui"
/>
<
role
rolename
=
"admin-script"
/>
<
role
rolename
=
"manager-gui"
/>
<
role
rolename
=
"manager-script"
/>
<
role
rolename
=
"manager-jmx"
/>
<
role
rolename
=
"manager-status"
/>
<
user
username
=
"admin"
password
=
"admin"
roles
=
"manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"
/>
2.Maven的Server的配置:在Maven的安装路径找到conf目录下的setting.xml文件,在<servers>节点中添加tomcat下配置的用户信息(id可以任意填写,但username和password必须和步骤1一致):
<
server
>
<
id
>tomcat</
id
>
<
username
>admin</
username
>
<
password
>admin</
password
>
</
server
>
3.在Maven项目的pom.xml文件的<plugins>节点中,添加tomcat的maven插件
tomcat7 maven插件配置(兼容tomcat8):
<
plugin
>
<
groupId
>org.apache.tomcat.maven</
groupId
>
<
artifactId
>tomcat7-maven-plugin</
artifactId
>
<
version
>2.2</
version
>
<
configuration
>
<!-- 注意tomcat7此处的url -->
<
url
>http://localhost:8080/manager/text</
url
>
<!-- 此处的名字必须和setting.xml中配置的ID一致-->
<
server
>tomcat</
server
>
<!-- 此处的名字是项目发布的工程名-->
<
path
>/ui</
path
>
</
configuration
>
</
plugin
>
tomcat6 maven插件配置:
<
plugin
>
<
groupId
>org.codehaus.mojo</
groupId
>
<
artifactId
>tomcat-maven-plugin</
artifactId
>
<
version
>1.1</
version
>
<
configuration
>
<!-- 注意tomcat此处的url -->
<
url
>http://localhost:8080/manager/html</
url
>
<!-- 此处的名字必须和setting.xml中配置的ID一致-->
<
server
>tomcat</
server
>
<!-- 此处的名字是项目发布的工程名-->
<
path
>/ui</
path
>
</
configuration
>
</
plugin
>
4.最后,进入项目pom.xml文件所在的目录执行maven命令:mvn tomcat7:deploy就可以了(必须事先启动tomcat服务器);在eclipse下maven项目构建自动部署web项目到tomcat如图:
初次部署用的是tomcat7:deploy命令,重新部署可以用tomcat7:redeploy命令(推荐统一用这个),Tomcat Maven插件支持的命令包括:run、shutdown、run-war-only、exec-war、standalone-war-only、deploy、standalone-war、undeploy、run-war、redeploy等。
5.最后日志如下表示部署成功:
[INFO] --- tomcat7-maven-plugin:2.2:redeploy (default-cli) @ ui ---
[INFO] Deploying war to http://172.16.100.177:9098/ui
Uploading: http://172.16.100.177:9098/manager/text/deploy?path=%2Fui&update=true
Uploaded: http://172.16.100.177:9098/manager/text/deploy?path=%2Fui&update=true (48931 KB at 11801.8 KB/sec)
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /ui
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:02 min
[INFO] Finished at: 2016-12-10T14:37:43+08:00
[INFO] Final Memory: 16M/98M
[INFO] ------------------------------------------------------------------------