Ant deploy war file to Tomcat server

雖然離開Java有段時間了
不過還是難忘Java在一些地方的好處
這邊簡介如何透過ANT來作Tomcat的部署
Tomcat主機資訊如下:

Tomcat version: 6.0.32
Server IP: xxx.xxx.xxx.xxx
Tomcat Port: 8080
Script Deployer: tomcat (password: tomcatpassword)

特殊設定:
1. ant需要把catalina-ant.jar放到ant家目錄的lib中
2. tomcat-user.xml(位於$TOMCAT_HOME/conf下)需要加入manager-script的角色

下面是deploy的ant script
裡面有hard code部署時候需要的
1. application name: /TestWeb2
2. server deploy endpoint: http://xxx.xxx.xxx.xxx:8080/manager
3. deployer username, password: tomcat / tomcatpassword
當然,這些變數也可以透過-Dkeyname=keyvalue的方式在執行時候帶入

build.xml
<project name="My Application" default="compile" basedir=".">
  <!-- Configure the context path for this application -->
  <property name="path"     value="/TestWeb2"/>

  <!-- Configure properties to access the Manager application -->
  <property name="url"      value="http://xxx.xxx.xxx.xxx:8080/manager"/>
  <property name="username" value="tomcat"/>
  <property name="password" value="tomcatpassword"/>

  <!-- Configure the custom Ant tasks for the Manager application -->
  <taskdef name="deploy"    classname="org.apache.catalina.ant.DeployTask"/>
  <taskdef name="list"      classname="org.apache.catalina.ant.ListTask"/>
  <taskdef name="reload"    classname="org.apache.catalina.ant.ReloadTask"/>
  <taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask"/>
  <taskdef name="roles"     classname="org.apache.catalina.ant.RolesTask"/>
  <taskdef name="start"     classname="org.apache.catalina.ant.StartTask"/>
  <taskdef name="stop"      classname="org.apache.catalina.ant.StopTask"/>
  <taskdef name="undeploy"  classname="org.apache.catalina.ant.UndeployTask"/>

  <target name="deploy" description="Install web application" >
    <deploy url="${url}" username="${username}" password="${password}" path="${path}" war="${war}"/>
  </target>

  <target name="reload" description="Reload web application">
    <reload  url="${url}" username="${username}" password="${password}" path="${path}"/>
  </target>

  <target name="undeploy" description="Remove web application">
    <undeploy url="${url}" username="${username}" password="${password}" path="${path}"/>
  </target>
</project>


執行範例:
Do Deploy
○ → ant -Dwar=/Users/simonsu/Desktop/TestWeb2.war deploy
Buildfile: /Users/simonsu/opt/tomcat-deployer/build.xml
Trying to override old definition of datatype resources

deploy:
   [deploy] OK - Deployed application at context path /TestWeb2

BUILD SUCCESSFUL
Total time: 0 seconds


Do Reload
○ → ant reload
Buildfile: /Users/simonsu/opt/tomcat-deployer/build.xml
Trying to override old definition of datatype resources

reload:
   [reload] OK - Reloaded application at context path /TestWeb2

BUILD SUCCESSFUL
Total time: 0 seconds


Do Undeploy
○ → ant undeploy
Buildfile: /Users/simonsu/opt/tomcat-deployer/build.xml
Trying to override old definition of datatype resources

undeploy:
 [undeploy] OK - Undeployed application at context path /TestWeb2

BUILD SUCCESSFUL
Total time: 0 seconds

參考資料:
http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#Executing_Manager_Commands_With_Ant