在之前的篇幅中提到了maven的配置、功能及简单的demo部署,本篇针对tomcat6 、tomcat7使用maven deploy war包的方法分别做下总结。配置之前需要准备好maven3 、tomcat6、tomcat7,并为maven安装好Maven-Tomcat plugin 插件。tomcat两个版本的deploy url 和命令分别如下:

1# Tomcat 7
2Deploy URL = http://localhost:8080/manager/text
3Command = mvn tomcat7:deploy
4# Tomcat 6
5Deploy URL = http://localhost:8080/manager/
6Command = mvn tomcat6:deploy

一、Tomcat7配置示例

1、配置tomcat认证

在tomcat manager中增加一个用户并配置在manager-gui 和 manager-script组中:

1# cat %TOMCAT7_PATH%/conf/tomcat-users.xml
2<?xml version='1.0' encoding='utf-8'?>
3<tomcat-users>
4	<role rolename="manager-gui"/>
5	<role rolename="manager-script"/>
6	<user username="admin" password="password" roles="manager-gui,manager-script" />
7</tomcat-users>

2、maven认证配置

在maven的setting配置文件中增加刚刚配置的用户信息,如下:

 1# %MAVEN_PATH%/conf/settings.xml
 2<?xml version="1.0" encoding="UTF-8"?>
 3<settings ...>
 4	<servers>
 5		<server>
 6			<id>TomcatServer</id>
 7			<username>admin</username>
 8			<password>password</password>
 9		</server>
10	</servers>
11</settings>

3、pom文件配置

在pom.xml 文件中配置tomcat7-maven-plugin的调用,并指定发布的URL与应用路径:

 1# cat pom.xml
 2<plugin>
 3		<groupId>org.apache.tomcat.maven</groupId>
 4		<artifactId>tomcat7-maven-plugin</artifactId>
 5		<version>2.2</version>
 6		<configuration>
 7			<url>http://localhost:8080/manager/text</url>
 8			<server>TomcatServer</server>
 9			<path>/MyWebApp</path>
10		</configuration>
11	</plugin>

4、deploy发布

发布相关指令:

1mvn tomcat7:deploy
2mvn tomcat7:undeploy
3mvn tomcat7:redeploy

初次部署用的是tomcat7:deploy命令,重新部署可以用tomcat7:redeploy命令(推荐统一用这个),Tomcat Maven插件支持的命令包括:run、shutdown、run-war-only、exec-war、standalone-war-only、deploy、standalone-war、undeploy、run-war、redeploy等。

具体发布示例:

 1> mvn tomcat7:deploy
 2...
 3[INFO] Deploying war to http://localhost:8080/mkyongWebApp
 4Uploading: http://localhost:8080/manager/text/deploy?path=%2FMyWebApp&update=true
 5Uploaded: http://localhost:8080/manager/text/deploy?path=%2FMyWebApp&update=true (13925 KB at 35250.9 KB/sec)
 6[INFO] tomcatManager status code:200, ReasonPhrase:OK
 7[INFO] OK - Deployed application at context path /MyWebApp
 8[INFO] ------------------------------------------------------------------------
 9[INFO] BUILD SUCCESS
10[INFO] ------------------------------------------------------------------------
11[INFO] Total time: 8.507 s
12[INFO] Finished at: 2014-03-18T11:35:25+08:00
13[INFO] Final Memory: 28M/308M
14[INFO] ------------------------------------------------------------------------

二、Tomcat6 deploy示例

1、配置tomcat认证

1# cat  %TOMCAT6_PATH%/conf/tomcat-users.xml
2<?xml version='1.0' encoding='utf-8'?>
3<tomcat-users>
4	<role rolename="manager-gui"/>
5	<role rolename="manager-script"/>
6	<user username="admin" password="password" roles="manager-gui,manager-script" />
7</tomcat-users>

2、配置maven认证

 1# cat %MAVEN_PATH%/conf/settings.xml
 2<?xml version="1.0" encoding="UTF-8"?>
 3<settings ...>
 4	<servers>
 5		<server>
 6			<id>TomcatServer</id>
 7			<username>admin</username>
 8			<password>password</password>
 9		</server>
10	</servers>
11</settings>

3、tomcat maven插件配置

 1#编辑pom.xml 文件增加如下内容
 2<plugin>
 3		<groupId>org.apache.tomcat.maven</groupId>
 4		<artifactId>tomcat6-maven-plugin</artifactId>
 5		<version>2.2</version>
 6		<configuration>
 7			<url>http://localhost:8080/manager</url>
 8			<server>TomcatServer</server>
 9			<path>/mkyongWebApp</path>
10		</configuration>
11	</plugin>

4、deploy发布

1mvn tomcat6:deploy
2mvn tomcat6:undeploy
3mvn tomcat6:redeploy

发布示例:

 1> mvn tomcat6:deploy
 2...
 3[INFO] Deploying war to http://localhost:8080/MyWebApp
 4Uploading: http://localhost:8080/manager/deploy?path=%2FMyWebApp
 5Uploaded: http://localhost:8080/manager/deploy?path=%2FMyWebApp (13925 KB at 32995.5 KB/sec)
 6[INFO] ------------------------------------------------------------------------
 7[INFO] BUILD SUCCESS
 8[INFO] ------------------------------------------------------------------------
 9[INFO] Total time: 22.652 s
10[INFO] Finished at: 2014-03-18T12:18:54+08:00
11[INFO] Final Memory: 30M/308M
12[INFO] ------------------------------------------------------------------------

三、可能遇到的问题

1、redeploy无法删旧的目录内容

具体在$TOMCAT_HOME/logs下的catalina日志,如下:

1信息: Undeploying context [/web-loab]
2Mar 18, 2014 13:52:26 下午 org.apache.catalina.startup.ExpandWar deleteDir
3严重: [D:\tomcat\apache-tomcat-7.0.56\webapps\web-loab\WEB-INF] could not be completely deleted. The presence of the remaining files may cause problems

这是因为Tomcat还在使用这个目录,无法删除,必须修改$TOMCAT_HOME/conf/context.xml:

1<Context antiJARLocking="true" antiResourceLocking="true">

2、Servelt.class offending

deploy以后web项目无法启动,具体报错如下:

1Mar 18, 2014 13:46:29 下午 org.apache.catalina.loader.WebappClassLoader validateJarFile
2信息: validateJarFile(D:\tomcat\apache-tomcat-7.0.56\webapps\web-loab\WEB-INF\lib\servlet-api-6.0.29.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class

原因是webapps目录下的某个Web项目的WEB-INF/lib目录下有servlet-api.jar,删除之,并在pom.xml中指定servelt-api.jar的scope为provided:

1<dependency>
2    <groupId>org.apache.tomcat</groupId>
3    <artifactId>servlet-api</artifactId>
4    <version>6.0.29</version>
5    <scope>provided</scope>
6</dependency>

四、总结

maven的deploy功能虽然不错,不过还是有一此局限。要求从本地开发环境能直接访问Tomcat服务器所在网段;并且要求tomcat开启manager功能---该功能本身就是把双刃剑,用不好也会存在安全隐患;而且不能保存历史版本包。所以相较不如目前公司使用的bamboo方案:先将包打好为zip包后,根据版本统一存放在内部的一台http服务器上,再通过内部脚本实现的deploy系统实现停tomcat、删除旧文件、解压新文件到tomcat app目录、更新配置文件、再次启动tomcat的步骤 。同时所有的tomcat也都阉割了manager功能,在一定程度上也杜绝了一些安全隐患。

不过bamboo内部也是通过配置调用如maven、ant这类工具来实现的,而且maven本身的deploy功能也并非一无是处,在开发人员测试代码时就比较好用。

参考页面

  1. Apache Tomcat 7 Manager App HOW-TO
  2. Apache Tomcat 6 Manager App HOW-TO
  3. Tomcat Maven Plugin
  4. Tomcat Maven Plugin – Context Goals