Bogdan Gusiev's blog

How to make good software for people


Ant task to install tomcat service26px
28 Apr 2009

If you are familiar to run Apache Tomcat as Windows service, you can install it with the ant task. build.xml file sample under the cut line.
First of all let's do some properties setup:
catalina.home=d:/java/tomcat-6.0.18
tomcat.executable=${catalina.home}/bin/tomcat6.exe
tomcat.service.executable=\
${catalina.home}/bin/service.bat
tomcat.service.name=myproject
# parameters that will be used in the jvm
tomcat.service.jvm.ms=128
tomcat.service.jvm.mx=256
Service startup task consists of 5 exec commands:
  • Stop service if currently running
  • Delete service if installed
  • Install service with bat script
  • Update service with some parameters
  • Start service
Here is the implementation:

    <target name="tomcat.service.install" description="Install tomcat service">
        
        <exec command="net stop ${tomcat.service.name}"
              failifexecutionfails="false"
              failonerror="false"/>

        <exec command="${tomcat.executable} //DS//${tomcat.service.name}"
              failifexecutionfails="false"
              failonerror="false"/>
        <exec executable="${tomcat.service.executable}" 
                  failifexecutionfails="true" failonerror="true">
            <env key="CATALINA_HOME" value="${catalina.home}"/>
            <arg value="install"/>
            <arg value="${tomcat.service.name}"/>
        </exec>
       <exec executable="${tomcat.executable}">
            <arg value="//US//${tomcat.service.name}"/>
            <arg value="--JvmMs=${jvm.ms}"/>
            <arg value="--JvmMx=${jvm.mx}"/>
            <arg value="--Startup=auto"/>
        </exec>
        <exec command="net start ${tomcat.service.name}" 
                  failifexecutionfails="true" failonerror="true"/>
    </target>

java howto ant tomcat26px windows service