ANT控制Tomacat若干问题和解决方案

使用ant对tomcat中的网站进行部署和更新,简单的方案就是把webapps下对应的目录和war文件删除,然后复制进新的war就可以了。

还有一种方案就是使用tomcat中提供的catalina-ant.jar里面提供了ant操作tomcat的一些操作

首先要在tomcat-users.xml 保证你有manager-scrpit的权限,注意不是manager-gui(因为这个权限对跨站攻击进行了保护,使用ANT访问会出现403错误)

然后你需要一个tomcatTasks.properties文件,内容如下(注意每行的结尾没有任何空格)

   1: deploy=org.apache.catalina.ant.DeployTask
   2: undeploy=org.apache.catalina.ant.UndeployTask
   3: reload=org.apache.catalina.ant.ReloadTask
   4: start=org.apache.catalina.ant.StartTask
   5: stop=org.apache.catalina.ant.StopTask
   6: list=org.apache.catalina.ant.ListTask
然后在你的配置文件中加入以下内容
   1: <path id="catalina-ant-classpath">
   2:     <fileset dir="${tomcat.home}/lib">
   3:         <include name="catalina-ant.jar"/>
   4:         <include name="tomcat-coyote.jar"/>
   5:         <include name="tomcat-util.jar"/>
   6:     </fileset>
   7:     <fileset dir="${tomcat.home}/bin">
   8:                 <include name="tomcat-juli.jar"/>
   9:     </fileset>
  10: </path>
  11:  
  12: <taskdef file="tomcatTasks.properties">
  13:     <classpath refid="catalina-ant-classpath"/>
  14: </taskdef>

还有几个属性需要设置,大家自己完善其中需要注意的是tomcat.manager.url,可以参考下面

   1: tomcat.manager.url=http://127.0.0.1:8080/manager/text

这样就可以参考以下代码,使用tomcat的API操作tomcat了

   1: <target name="install" description="Install application in Tomcat" depends="packWar">
   2:         <deploy url="${tomcat.manager.url}" 
   3:                 username="${tomcat.manager.username}" 
   4:                 password="${tomcat.manager.password}" 
   5:                 path="/${webapp.name}" war="${warFilePath}" />
   6:     </target>
   7:  
   8:     <target name="remove" description="Remove application from Tomcat">
   9:         <undeploy url="${tomcat.manager.url}" 
  10:                     username="${tomcat.manager.username}" 
  11:                     password="${tomcat.manager.password}" 
  12:                     path="/${webapp.name}" />
  13:     </target>
  14:  
  15:     <target name="reload" description="Reload application in Tomcat">
  16:         <reload url="${tomcat.manager.url}" 
  17:                 username="${tomcat.manager.username}" 
  18:                 password="${tomcat.manager.password}" 
  19:                 path="/${webapp.name}" />
  20:     </target>
  21:  
  22:     <target name="start" description="Start Tomcat application">
  23:         <start url="${tomcat.manager.url}" 
  24:                 username="${tomcat.manager.username}" 
  25:                 password="${tomcat.manager.password}" 
  26:                 path="/${webapp.name}" />
  27:     </target>
  28:  
  29:     <target name="stop" description="Stop Tomcat application">
  30:         <stop url="${tomcat.manager.url}" 
  31:                 username="${tomcat.manager.username}" 
  32:                 password="${tomcat.manager.password}" 
  33:                 path="/${webapp.name}" />
  34:     </target>
  35:  
  36:     <target name="list" description="List Tomcat applications">
  37:         <list url="${tomcat.manager.url}" 
  38:             username="${tomcat.manager.username}" 
  39:             password="${tomcat.manager.password}" />
  40:     </target>

 

但是

windows下的tomcat7会因为内存泄露的问题而有时候文件删除不掉(使用catalina-ant也会undeploy失败)!!!

比较极限的解决方案是是关闭tomcat,删除文件后在重启。相应的ant配置如下

   1: <target name="stop_tomcat">  
   2:     <echo>停止tomcat</echo>  
   3:     <exec executable="cmd" dir="${tomcat.home}/bin" failonerror="false"   
   4:                 output="${log.file}" append="true" >   
   5:         <env key="CATALINA_HOME" path="${tomcat.home}"/>  
   6:         <arg value="/c shutdown.bat" />     
   7:     </exec>  
   8: </target>  
   9:   
  10: <target name="start_tomcat">  
  11:     <echo>启动tomcat</echo>  
  12:     <exec executable="cmd" dir="${tomcat.home}/bin"  failonerror="false"   
  13:                 output="${log.file}" append="true" >    
  14:          <env key="CATALINA_HOME" path="${tomcat.home}"/>  
  15:          <arg value="/c startup.bat" />     
  16:     </exec>  
  17: </target>  

还有一个比较好的做法是修改tomcat的配置修改conf/context.xml中的内容修改成

   1: <Context antiResourceLocking="true">

你可能感兴趣的:(解决方案)