今天研究对象是Ant,用于对msms系统进行Tomcat的自动部署。
遇到一个怪问题,百思不得其解:
通过ant depoly可以把msms.war部署到tomcat上。当然,要求这时候webapps下面没有msms目录。
看tomcat的纪录,加载msms.war正常,网页也可以打开。
通过ant undeploy,可以把msms卸载。Tomcat显示:
Undeploying context [/msms]
正常卸载了。问题是,去看webapps目录下面,居然有一个msms目录的残骸,里面保留的目录是
WEB-INF\lib
有以下几个文件残留着:
commons-digester.jar
commons-validator.jar
struts.jar
这时候msms目录也无法手工删除,必须停掉tomcat后才能删除。
我装的tomcat是5.5。不知道是否还有人遇到过这种情况,我反正是晕了。
build.properties文件如下:
tomcat.dir=C:/ApacheGroup/Tomcat5.5
tomcat.webapps=C:/ApacheGroup/Tomcat5.5/webapps
tomcat.manager.url=http://localhost:8080/manager
tomcat.manager.username=admin
tomcat.manager.password=xxxxxxxx
build.xml文件如下:
<?xml version="1.0"?>
<project name="msms" default="compile" basedir=".">
<!-- Ant Tomcat Task Definition -->
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask">
<classpath>
<path location="${tomcat.dir}/server/lib/catalina-ant.jar" />
</classpath>
</taskdef>
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask">
<classpath>
<path location="${tomcat.dir}/server/lib/catalina-ant.jar" />
</classpath>
</taskdef>
<taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
<classpath>
<path location="${tomcat.dir}/server/lib/catalina-ant.jar" />
</classpath>
</taskdef>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
<classpath>
<path location="${tomcat.dir}/server/lib/catalina-ant.jar" />
</classpath>
</taskdef>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
<classpath>
<path location="${tomcat.dir}/server/lib/catalina-ant.jar" />
</classpath>
</taskdef>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
<classpath>
<path location="${tomcat.dir}/server/lib/catalina-ant.jar" />
</classpath>
</taskdef>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
<classpath>
<path location="${tomcat.dir}/server/lib/catalina-ant.jar" />
</classpath>
</taskdef>
<property name="build" value="build" />
<property name="dist" value="dist" />
<property name="src" value="src/java" />
<property name="test" value="src/test" />
<property name="war-config" value="src/config" />
<property name="report" value="report" />
<property name="lib" value="lib" />
<property name="web" value="web" />
<property name="meta" value="meta" />
<property name="context-path" value="${ant.project.name}" />
<property file="build.properties" />
<path id="build.classpath">
<fileset file="${lib}/*.jar" />
<fileset dir="${tomcat.dir}/common/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.dir}/common/endorsed">
<include name="*.jar" />
</fileset>
<pathelement path="${build}" />
</path>
<!-- Hibernate Tool Task Definition -->
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="build.classpath" />
<target name="clean">
<echo message="Cleaning up the build and dist directories" />
<delete dir="${build}" />
<mkdir dir="${build}" />
<delete dir="${dist}" />
<mkdir dir="${dist}" />
</target>
<target name="copy-resources">
<copy todir="${build}">
<fileset dir="${src}">
<exclude name="**/*.java" />
<exclude name="**/*.hbm.xml" />
</fileset>
</copy>
</target>
<target name="compile" depends="copy-resources">
<javac destdir="${build}" srcdir="${src}:${test}">
<classpath refid="build.classpath" />
</javac>
</target>
<target name="initdb" depends="compile">
<hibernatetool destdir="${build}">
<classpath>
<path location="${build}" />
</classpath>
<annotationconfiguration configurationfile="src/java/hibernate.cfg.xml" />
<hbm2ddl create="true" />
</hibernatetool>
</target>
<target name="run" depends="compile">
<java fork="true" classname="cn.ac.rcpa.msms.tools.ProjectManager" classpathref="build.classpath">
<classpath path="${build}" />
<arg value="${action}" />
<arg value="${project}" />
<arg value="${description}" />
</java>
</target>
<target name="test" depends="compile" description="run junit test">
<delete dir="${report}" />
<mkdir dir="${report}" />
<junit dir="." fork="true" printsummary="on" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
<classpath refid="build.classpath" />
<formatter type="brief" />
<batchtest todir="${report}">
<fileset dir="${build}">
<include name="**/*Test.*" />
<include name="**/Test*.*" />
</fileset>
</batchtest>
</junit>
<fail if="tests.failed">
***********************************************************
**** One or more tests failed! Check the output ... ****
***********************************************************
</fail>
</target>
<target name="create-war" depends="clean, compile" description="build release war">
<echo message="creation the WAR file...${context-path}.war" />
<war destfile="${dist}/${context-path}.war" webxml="${meta}/web.xml">
<classes dir="${build}">
<exclude name="**/*Test.*" />
<exclude name="**/Test*.*" />
<exclude name="hibernate.cfg.xml" />
</classes>
<lib dir="${lib}" />
<fileset dir="${web}" />
<zipfileset dir="${war-config}" prefix="WEB-INF/classes" />
</war>
<!-- <scp file="${dist}/${context-path}.war" todir="root:${password}@172.16.1.20:/usr/local/tomcat/webapps" trust="true" /> -->
</target>
<target name="deploy" description="Install application in Tomcat">
<deploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${context-path}" localWar="file:${dist}/${context-path}.war" />
</target>
<target name="undeploy" description="Remove application in Tomcat" if="already.deployed">
<undeploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${context-path}" />
</target>
<target name="reload" description="Reload application in Tomcat">
<reload url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${context-path}" />
</target>
<target name="start" description="Start Tomcat application">
<start url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${context-path}" />
</target>
<target name="stop" description="Stop Tomcat application">
<stop url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${context-path}" />
</target>
<target name="list" description="List Tomcat applications">
<list url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" />
</target>
<target name="deploy-application" description="Compile the web application...">
<echo message="Undeploying the application only if it's deployed..." />
<available file="${tomcat.webapps}/${context-path}.war" property="already.deployed" />
<antcall target="undeploy" />
<antcall target="create-war" />
<antcall target="deploy" />
</target>
</project>
只有Windows上才会有的问题,
创建META-INF目录, 在目录里新建context.xml,加入如下内容
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true" antiResourceLocking="true">
</Context>
昨天的错误至今无法修正,即使重装了Tomcat也不行。
不过,deploy到两台linux服务器上倒都成功了。
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask">
<classpath>
<path location="${tomcat.dir}/server/lib/catalina-ant.jar" />
</classpath>
</taskdef>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
<classpath>
<path location="${tomcat.dir}/server/lib/catalina-ant.jar" />
</classpath>
</taskdef>
<target name="deploy-web" description="Install application in Local Tomcat">
<echo message="deploying to web ..." />
<deploy url="http://172.16.1.20:8080/manager" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${context-path}" war="file:${dist}/${context-path}.war" update="true" />
</target>
<target name="list-web">
<echo message="list web tomcat ..." />
<list url="http://172.16.1.20:8080/manager" username="${tomcat.manager.username}" password="${tomcat.manager.password}" />
</target>
这里跟昨天的代码有一点不一样:
1、localWar改成了war。
原来更新远程服务器的时候,总是显示更新成功,但是到webapps目录下总是没有相应的war文件,通过调用
ant list-web
发现,有一个context-path是dist/msms,而且是stop状态。哦,原来localWar的意思不是指开发的机器的local path,而是指把这个war被写到服务器的什么地方。改成war就正常了。
2、不需要判断是否已经deploy进而调用undeploy了,直接通过update="true"更新就可以了。