背景:
客户端:xp(192.168.1.102) +eclipse自带的ant
服务器:linux(192.168.1.100) +tomcat6
通过客户端的ant远程部署项目到linux服务器
Step1) 配置客户端
将服务端的${CATALINA_HOME}/lib/catalina-ant.jar拷贝到客户端的${ANT_HOME}/lib
客户端的${ANT_HOME}在本文中指的是D:/dev/eclipse/plugins/org.apache.ant_1.6.5/lib
即eclipse自带的ant目录,为了让eclipse自带的ant能正常运行任务,你还得将这个jar添加到eclipse的ant的classpath中(windows-->preferences-->Ant-->runtime-->classpath)
你完全可以不使用eclipse自带的ant,上面的步骤显得很麻烦,你完全可以开启dos窗口运行ant,但这样的话你就的两边切换窗口,一边在eclipse下写代码,一边在dos下运行ant,根据的自己的实际情况取舍吧
Step2) 配置服务器
我们主要通过tomcat manager api来完成部署工作,这个需要权限,所以你的修改
$CATALINA_HOME/conf/tomcat-users.xml加入
<
user name
=
"
craigmcc
"
password
=
"
secret
"
roles
=
"
manager
"
/>
你可以通过手动访问下面的地址
如果成功,说明你的服务端配置正确
Step3)客户端脚本
<
project name
=
"
My Application
"
default
=
"
war
"
basedir
=
"
.
"
>
<!-- Configure the directory into which the web application is built -->
< property name = " build " value = " ${basedir}/webcontent/WEB-INF/classes " />
< property name = " dist.dir " value = " ${basedir}/dist " />
<!-- Configure the context path for this application -->
< property name = " path " value = " /search " />
<!-- Configure properties to access the Manager application -->
< property name = " url " value = " http://192.168.1.100/manager " />
< property name = " username " value = " weip " />
< property name = " password " value = " 123 " />
<!-- Configure the custom Ant tasks for the Manager application -->
< taskdef name = " deploy " classname = " org.apache.catalina.ant.DeployTask " />
< taskdef name = " undeploy " classname = " org.apache.catalina.ant.UndeployTask " />
<!-- Executable Targets -->
。。。。。。。。。。。。。。。。。。。。
< target name = " war " description = " package web application " >
< war destfile = " ${dist.dir}/search.war " webxml = " webcontent/WEB-INF/web.xml " >
< classes dir = " webcontent/WEB-INF/classes " />
< fileset dir = " webcontent " excludes = " WEB-INF/** " />
< lib dir = " webcontent/WEB-INF/lib " />
</ war >
</ target >
< target name = " deploy " description = " Install web application " depends = " war " >
< deploy url = " ${url} " username = " ${username} " password = " ${password} " path = " ${path} " war = " ${dist.dir}/search.war " />
</ target >
< target name = " undeploy " description = " Remove web application " >
< undeploy url = " ${url} " username = " ${username} " password = " ${password} " path = " ${path} " />
</ target >
</ project >
<!-- Configure the directory into which the web application is built -->
< property name = " build " value = " ${basedir}/webcontent/WEB-INF/classes " />
< property name = " dist.dir " value = " ${basedir}/dist " />
<!-- Configure the context path for this application -->
< property name = " path " value = " /search " />
<!-- Configure properties to access the Manager application -->
< property name = " url " value = " http://192.168.1.100/manager " />
< property name = " username " value = " weip " />
< property name = " password " value = " 123 " />
<!-- Configure the custom Ant tasks for the Manager application -->
< taskdef name = " deploy " classname = " org.apache.catalina.ant.DeployTask " />
< taskdef name = " undeploy " classname = " org.apache.catalina.ant.UndeployTask " />
<!-- Executable Targets -->
。。。。。。。。。。。。。。。。。。。。
< target name = " war " description = " package web application " >
< war destfile = " ${dist.dir}/search.war " webxml = " webcontent/WEB-INF/web.xml " >
< classes dir = " webcontent/WEB-INF/classes " />
< fileset dir = " webcontent " excludes = " WEB-INF/** " />
< lib dir = " webcontent/WEB-INF/lib " />
</ war >
</ target >
< target name = " deploy " description = " Install web application " depends = " war " >
< deploy url = " ${url} " username = " ${username} " password = " ${password} " path = " ${path} " war = " ${dist.dir}/search.war " />
</ target >
< target name = " undeploy " description = " Remove web application " >
< undeploy url = " ${url} " username = " ${username} " password = " ${password} " path = " ${path} " />
</ target >
</ project >
开始运行
第一次部署
结果:
2007
-
7
-
30
22
:
35
:
59
org.apache.catalina.startup.HostConfig deployWAR
信息: Deploying web application archive search.war
2007 - 07 - 30 22 : 36 : 03 , 595 INFO XmlConfigurationProvider - Parsing configuration file [struts - default .xml]
2007 - 07 - 30 22 : 36 : 03 , 887 INFO XmlConfigurationProvider - Parsing configuration file [struts - plugin.xml]
2007 - 07 - 30 22 : 36 : 03 , 923 INFO XmlConfigurationProvider - Parsing configuration file [struts.xml]
2007 - 07 - 30 22 : 36 : 04 , 140 INFO ObjectTypeDeterminerFactory - Detected GenericsObjectTypeDeterminer, initializing it...
2007 - 07 - 30 22 : 36 : 04 , 146 INFO XWorkConverter - Detected AnnotationXWorkConverter, initializing it...
信息: Deploying web application archive search.war
2007 - 07 - 30 22 : 36 : 03 , 595 INFO XmlConfigurationProvider - Parsing configuration file [struts - default .xml]
2007 - 07 - 30 22 : 36 : 03 , 887 INFO XmlConfigurationProvider - Parsing configuration file [struts - plugin.xml]
2007 - 07 - 30 22 : 36 : 03 , 923 INFO XmlConfigurationProvider - Parsing configuration file [struts.xml]
2007 - 07 - 30 22 : 36 : 04 , 140 INFO ObjectTypeDeterminerFactory - Detected GenericsObjectTypeDeterminer, initializing it...
2007 - 07 - 30 22 : 36 : 04 , 146 INFO XWorkConverter - Detected AnnotationXWorkConverter, initializing it...
第二次部署出现错误:
deploy:
[deploy] FAIL - Application already exists at path /search
[deploy] FAIL - Application already exists at path /search
于是将
<
target name
=
"
deploy
"
description
=
"
Install web application
"
depends
=
"
war
"
>
。。。。。。。。
</ target >
。。。。。。。。
</ target >
变为
<
target name
=
"
deploy
"
description
=
"
Install web application
"
depends
=
"
war,undeploy
"
>
。。。。。。。。
</ target >
。。。。。。。。
</ target >
结果:
信息: Undeploying context [
/
search]
2007 - 7 - 30 22 : 46 : 27 org.apache.catalina.startup.HostConfig deployWAR
信息: Deploying web application archive search.war
2007 - 07 - 30 22 : 46 : 29 , 926 INFO XmlConfigurationProvider - Parsing configuration file [struts - default .xml]
2007 - 07 - 30 22 : 46 : 30 , 048 INFO XmlConfigurationProvider - Parsing configuration file [struts - plugin.xml]
2007 - 07 - 30 22 : 46 : 30 , 083 INFO XmlConfigurationProvider - Parsing configuration file [struts.xml]
2007 - 07 - 30 22 : 46 : 30 , 250 INFO ObjectTypeDeterminerFactory - Detected GenericsObjectTypeDeterminer, initializing it...
2007 - 07 - 30 22 : 46 : 30 , 252 INFO XWorkConverter - Detected AnnotationXWorkConverter, initializing it...
2007 - 7 - 30 22 : 46 : 27 org.apache.catalina.startup.HostConfig deployWAR
信息: Deploying web application archive search.war
2007 - 07 - 30 22 : 46 : 29 , 926 INFO XmlConfigurationProvider - Parsing configuration file [struts - default .xml]
2007 - 07 - 30 22 : 46 : 30 , 048 INFO XmlConfigurationProvider - Parsing configuration file [struts - plugin.xml]
2007 - 07 - 30 22 : 46 : 30 , 083 INFO XmlConfigurationProvider - Parsing configuration file [struts.xml]
2007 - 07 - 30 22 : 46 : 30 , 250 INFO ObjectTypeDeterminerFactory - Detected GenericsObjectTypeDeterminer, initializing it...
2007 - 07 - 30 22 : 46 : 30 , 252 INFO XWorkConverter - Detected AnnotationXWorkConverter, initializing it...
成功
总结:
本文主要通过ant脚本来远程部署tomcat,旨在做一个尝试,上面的脚本与这种部署方式都与实际的环境相差甚远,这种部署方式适合服务器和客户端没有程序配置差别的应用,一旦有了程序配置差别,这种方式就不合适了,你可以考虑采用在服务器段进行本地部署消除这种差别(对于复杂的部署环境也许是两者的结合),这样也有利于持续构建。