Maven deploy war到tomcat6、tomcat7

2014年3月18日 发表评论 阅读评论

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

# Tomcat 7
Deploy URL = http://localhost:8080/manager/text
Command = mvn tomcat7:deploy
# Tomcat 6
Deploy URL = http://localhost:8080/manager/
Command = mvn tomcat6:deploy

一、Tomcat7配置示例

1、配置tomcat认证

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

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

2、maven认证配置

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

# %MAVEN_PATH%/conf/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings ...>
	<servers>
		<server>
			<id>TomcatServer</id>
			<username>admin</username>
			<password>password</password>
		</server>
	</servers>
</settings>

3、pom文件配置

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

# cat pom.xml
<plugin>
		<groupId>org.apache.tomcat.maven</groupId>
		<artifactId>tomcat7-maven-plugin</artifactId>
		<version>2.2</version>
		<configuration>
			<url>http://localhost:8080/manager/text</url>
			<server>TomcatServer</server>
			<path>/MyWebApp</path>
		</configuration>
	</plugin>

4、deploy发布

发布相关指令:

mvn tomcat7:deploy
mvn tomcat7:undeploy
mvn 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等。

具体发布示例:

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

二、Tomcat6 deploy示例

1、配置tomcat认证

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

2、配置maven认证

# cat %MAVEN_PATH%/conf/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings ...>
	<servers>
		<server>
			<id>TomcatServer</id>
			<username>admin</username>
			<password>password</password>
		</server>
	</servers>
</settings>

3、tomcat maven插件配置

#编辑pom.xml 文件增加如下内容
<plugin>
		<groupId>org.apache.tomcat.maven</groupId>
		<artifactId>tomcat6-maven-plugin</artifactId>
		<version>2.2</version>
		<configuration>
			<url>http://localhost:8080/manager</url>
			<server>TomcatServer</server>
			<path>/mkyongWebApp</path>
		</configuration>
	</plugin>			

4、deploy发布

mvn tomcat6:deploy
mvn tomcat6:undeploy
mvn tomcat6:redeploy 

发布示例:

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

三、可能遇到的问题

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

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

信息: Undeploying context [/web-loab]
Mar 18, 2014 13:52:26 下午 org.apache.catalina.startup.ExpandWar deleteDir
严重: [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:

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

2、Servelt.class offending

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

Mar 18, 2014 13:46:29 下午 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: 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:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>servlet-api</artifactId>
    <version>6.0.29</version>
    <scope>provided</scope>
</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




本站的发展离不开您的资助,金额随意,欢迎来赏!

You can donate through PayPal.
My paypal id: itybku@139.com
Paypal page: https://www.paypal.me/361way

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.