论坛首页 Java企业应用论坛

ant tomcat 启动 关闭 注册成windows服务

浏览 8275 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-06   最后修改:2011-03-08

tomcat注册成windows服务:
打开cmd命令行,进入tomcat目标下的bin目录里,执行service.bat install tomcat_juziku(服务名)

 

如果想删除windows服务,可以进入cmd命令行,执行sc delete tomcat_juziku(其中,tomcat_juziku为服务名,如果服务名中间有空格,就需要前后加引号)

 

 

--------------------------------------------------------------------

问题: 自己写ant的时候build完war后想部署到tomcat上,然后启动tomcat,在启动前会先运行shutdown尝试关闭tomcat,并且删除掉之前部署的war和相关目录,结果运行shutdown之后马上尝试删除的时候,说是有些资源被使用,无法删除,应该是shutdown执行的关闭tomcat没有完全执行完,就开始执行删除,导致资源被使用中,在网上找了些相关的处理办法,记录如下:

 

1.

==========================================

ant 启动 tomcat

<target name="tomcat.start">
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
</java>
</target>

<target name="tomcat.stop">
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
<arg line="stop"/>
</java>
</target>

<target name="tomcat.debug">
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
<jvmarg value="-Xdebug"/>
<jvmarg
value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"/>
</java>
</target>

 

 

2、

=======================================================

今天研究对象是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"更新就可以了。


 

彻底解决Ant在Tomcat进行卸载部署undeploy时不能删除jar文件的问题 

首先有一种解决办法,就是在应用下的META-INF下新建context.xml: 
<?xml version="1.0" encoding="UTF-8"?> 
<Context reloadable="true" antiResourceLocking="true" antiJARLocking="true"> 
</Context> 

这种方法在重复进行多次deploy、undeploy时的确解决了该问题。但是如果在deploy和undeploy之间执行如下的 webtest任务,也就是在undeploy前访问该应用,那么Tomcat就会加载jar文件,再执行undeploy时还是不能删除jar文件,此 时似乎Tomcat植入jar根深蒂固了! 
<target name="webtest_login"> 
<echo message="WebTest测试"/> 
<exec executable="cmd"> 
<arg line="/c webtest.bat -f tools/Java/webtest/login.xml -Dwebtest.home=${tools.webtest}"/> 
<arg line="-Dwebtest.home=${tools.webtest}"/> 
<arg line="-Dhost=${manager.ip}"/> 
<arg line="-Dport=${manager.port}"/> 
<arg line="-Dbasepath=${deploy.name}"/> 
<arg line="-Dresultpath=${report.webtestReport.part}"/> 
<arg line="-Dresultfile=${report.webtestReport.xml.part}"/> 
</exec> 
<xslt in="${report.webtestReport.xml}" out="${report.webtestReport.html}" style="${tools.webtest.reportstyle}" /> 
</target> 

有一个办法可以彻底解决该问题,就是使用xml部署文件来部署应用,不适用war发布的方式: 
<target name="deploy"> 
<echo message="安装Web应用"/> 
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="classpath.catalina"/> 
<deploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="/${deploy.name}" config="D:\AutoBuilder\build\student\web\context.xml"/> 
</target> 

context.xml描述的项目目录位于Tomcat外的目录: 
<Context path="/student" docBase="D:/AutoBuilder/build/student/web" debug="0"/> 

这样应用程序是指向外界的,不位于Tomcat目录中,再执行undeploy时就没有这个问题了。而且使用war进行deploy时后台的错误也不会再出现了: 
java.io.FileNotFoundException: webapps\student (The system cannot find the path specified) 
at java.io.FileInputStream.open(Native Method) 
at java.io.FileInputStream.<init>(FileInputStream.java:106) 
at org.apache.catalina.startup.ExpandWar.copyInternal(ExpandWar.java:226) 

BTW,deploy时会在C:\Tomcat 5.5\conf\Catalina\localhost下拷贝生成context.xml,undeploy时会删除该文件

 

 

 

 

3、

=======================================================

启动

注意tomcat.home是你的tomcat的安装路径

 

<target name="start-tomcat" description="tomcat starting.....">
        <exec executable="${tomcat.home}/bin/startup.bat" spawn="true" vmlauncher="false">
        <env key="CATALINA_HOME" value="${tomcat.home}" />
        <arg line="/c start ${tomcat.home}/bin/startup.bat" />
      </exec>		
   </target>

 

 

 关闭

 

<target name="tomcat.stop">
	<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
		<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
		<arg line="stop"/>
	</java>
	<waitfor maxwait="5" maxwaitunit="second">
                                <available file="errors.log"/>
	</waitfor>

</target>

 

 

总结

自己试了下,用的第3种,其实他没有彻底解决我的问题,就是多了一个wait的时间,这个时间也是个估计值

 

 

 

 

 

 

 

 

liunx 下的  ant tomcat

===================================================================

最近公司项目上线了,这一段时间我频繁地按照Release Guide做重复性操作,不胜其烦。今天刚好有空,就用ant加Linux的sh文件做了一个自动Release的脚本,哈哈,以后方便了,一条命令就可以全部搞定。

    首先把ant解压到路径/opt/app下,然后用vi编辑/etc/profile文件,加入以下几个语句:
    export ANT_HOME=/opt/app/apache-ant-1.7.0
    export JAVA_HOME=/opt/app/jdk1.5.0_16
    export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin
    按Esc后同时按shift跟:键,接着输入命令wq退出vi。现在我们可以写ant脚本跟sh文件了

/*  release.sh  */
cd /opt/app/apache-tomcat-5.5.26/bin
./shutdown.sh
killall -9 java         //因为有时候shutdown不能完全关闭tomcat,所以就用这条命令把java线程全部kill掉

kill -TERM `ps -ef | grep 5.5.26 | grep -v grep | awk '{print $2}'`  //或者仅仅kill掉tomcat-5.5.26的线程
rm -rf /opt/app/apache-tomcat-5.5.26/work/Catalina/localhost
cd /opt/app/my
ant all
cd /opt/app/my_1.0_dynamic
unzip  my_dynamic.war
mv /opt/app/my/trunk/my.xml /opt/app/apache-tomcat-5.5.26/conf/Catalina/localhost/     //这个文件指定Context
cd /opt/app/apache-tomcat-5.5.26/bin
./startup.sh

/*  my.xml  */
<?xml version="1.0" encoding="UTF-8"?>

<Context docBase="/opt/app/my_1.0_dynamic" crossContext="true" >
</Context>

/*  build.properties  */
# -----------------------------------------------------------------------------
# build.properties
# This file is referenced by the build.xml file.
# -----------------------------------------------------------------------------

svnant.version=1.0.0

# -----------------------------------------------------------------------------
# all jar needed
# -----------------------------------------------------------------------------
svnlib.dir=trunk/lib/svnant
svnant.jar=${svnlib.dir}/svnant.jar
svnClientAdapter.jar=${svnlib.dir}/svnClientAdapter.jar
svnjavahl.jar=${svnlib.dir}/svnjavahl.jar

svnant.latest.url=http://192.168.1.1/svn/projects/my/coding/trunk/

svnant.repository.user=your username
svnant.repository.passwd=your password

dev.apache.DocumentRoot=/opt/app/httpd-2.2.9-ssl-rewrite/htdocs

/*  build.xml  */
<?xml version="1.0"?>

<project name="myProject" basedir="." default="usage">

    <!--  all properties are in build.properties -->
    <property file="build.properties" />

    <property name="name" value="${ant.project.name}"/>

    <property name="src.dir" value="trunk/src"/>
    <property name="src.webapp" value="trunk/web"/>
    <property name="lib.dir" value="trunk/lib"/>
    <property name="static.dir" value="trunk/static"/>
   
    <property name="build.dir" value="trunk/build"/>
    <property name="build.webapp" value="${build.dir}/webapp"/>
    <property name="build.classes" value="${build.webapp}/WEB-INF/classes"/>

    <property name="dist.dir" value="/opt/app/my_1.0_dynamic"/>

    <!-- path to the svnant libraries. -->
    <path id="svnant.classpath">
        <pathelement location="${svnjavahl.jar}" />
        <pathelement location="${svnant.jar}" />
        <pathelement location="${svnClientAdapter.jar}" />
    </path>
    <taskdef resource="svntask.properties" classpathref="svnant.classpath"/>   
   
    <!-- another way is to put all lib to ant_home/lib and just uncomment the following line. -->
    <!-- <taskdef resource="svntask.properties"/>-->
 
  <target name="checkout">
    <svn username="${svnant.repository.user}" password="${svnant.repository.passwd}">
      <checkout url="${svnant.latest.url}" revision="HEAD" destPath="trunk" />
    </svn>
  </target>

    <path id="master-classpath">
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/test">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/servlet">
            <include name="*.jar"/>
        </fileset>
    </path>

    <target name="usage">
        <echo message=""/>
        <echo message="my build"/>
        <echo message="------------------------------------------------------"/>
        <echo message=""/>
        <echo message="Available targets are:"/>
        <echo message=""/>
        <echo message="clean    --> Clean output dirs"/>
        <echo message="checkout --> Check out latest source from svn server"/>
        <echo message="build    --> Compile main Java sources and copy libraries"/>
        <echo message="war      --> Build a war file"/>
        <echo message="all      --> Clean, checkout,build, war, tests"/>
        <echo message=""/>
    </target>

    <target name="init" description="Clean output dirs (.classes and .testclasses)">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.webapp}"/>
        <mkdir dir="${build.webapp}/WEB-INF"/>
        <mkdir dir="${build.webapp}/WEB-INF/lib"/>
        <mkdir dir="${build.classes}"/>
    </target>
   
    <target name="clean" description="Clean output dirs (.classes and .testclasses)">
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
        <delete>
            <fileset dir="${src.webapp}/WEB-INF/lib" includes="**/*.jar"/>
        </delete>
        <delete dir="${src.webapp}/WEB-INF/classes/au"/>
    </target>

    <target name="build" depends="init" description="Compile main source tree java files into class files, generate jar files">
        <javac destdir="${build.classes}" source="1.5" target="1.5" debug="true"
               deprecation="false" optimize="false" failonerror="true" encoding="utf-8">
            <src path="${src.dir}"/>
        <classpath refid="master-classpath"/>
        </javac> 
    </target>

    <target name="webapp" depends="init, build">
        <copy todir="${build.webapp}">
            <fileset dir="${src.webapp}"/>
        </copy>
        <copy todir="${build.webapp}/WEB-INF/lib">
            <fileset dir="${lib.dir}">
                <exclude name="**/servlet*"/>
                <exclude name="**/servlet/*"/>
                <exclude name="**/test*"/>
                <exclude name="**/test/*.jar"/>
                <exclude name="**/svnant/*.jar"/>
                <exclude name="**/svnant*"/>
            </fileset>
        </copy>
        <copy todir="${build.webapp}/WEB-INF/classes">
            <fileset dir="${src.dir}">
                <include name="**/resources/*"/>
            </fileset>
        </copy>
        <copy todir="${build.webapp}/WEB-INF/classes">
            <fileset dir="${src.dir}">
                <include name="**/*validation.xml"/>
            </fileset>
        </copy>
        <copy todir="${dev.apache.DocumentRoot}">
            <fileset dir="${static.dir}">
            </fileset>
        </copy>
    </target>

    <target name="war" depends="init, build, webapp">
        <jar jarfile="${build.webapp}/WEB-INF/lib/my.jar" basedir="${build.webapp}/WEB-INF/classes" >
        <include name="**/au/**"/>
        <include name="**/com/**"/>
        </jar>
        <jar jarfile="${build.dir}/my_dynamic.war" basedir="${build.webapp}" >
                <exclude name="**/jai_*.jar"/>
        <exclude name="**/classes/au/**"/>
        <exclude name="**/classes/com/**"/>
        </jar>
  </target>

    <target name="dist" depends="init, build, webapp, war">
           <mkdir dir="${dist.dir}"/>
        <copy file="${build.dir}/my_dynamic.war" todir="${dist.dir}"/>
     </target>


    <target name="all" depends="clean,checkout,dist" description="Clean,checkout,build,warfile,dist"/>
   
</project>

 

 

 

 

 

   发表时间:2010-06-30  
  好羡慕啊,我现在用Ant 就是不厌其烦的重启和关掉Tomcat才能更新的部署项目.
我用的环境是windows啊,看了楼主的哪些方法,但是还是不怎么明白该怎么去配置.
0 请登录后投票
   发表时间:2010-06-30  
window下把tomcat安装成服务 是可以准确的在tomcat关闭完全后,然后重新部署,然后启动的,我没试过,以前有同事是这么做成功了的
0 请登录后投票
   发表时间:2010-10-19  
My conclusion is that your problem still not yet been solved, mess.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics