Start and Stop Tomcat service using Ant

2020-03-31 09:42发布

问题:

I need to start Tomcat application running as a service (application) in windows machine with an Ant buildfile. I am able to do the same with available batch file to start up and shutdown. But, now I want to make this happen without batch file.

Note: Now tomcat is running as application service

回答1:

You can do it this way:

  1. First Create a macro named service:

    <macrodef name="service">
    <attribute name="service" />
    <attribute name="action" />
    <sequential>
        <exec executable="cmd.exe">
            <arg line="/c net @{action} '@{service}'" />
        </exec>
    </sequential>
    

  2. Now create a task that uses the service macro:

    <property name="servicename" value="myWindowsServiceName" />
    <target name="start">           
       <service action="start" service="${servicename}" />
    </target>
    <target name="stop">
        <service action="stop" service="${servicename}" />
    <exec dir="." executable="cmd.exe">
        <arg line ="/c taskkill /f /fi 'services eq ${servicename}' " />
    </exec>
        <sleep seconds="5" />
    </target>
    <target name="restart" depends="stop,start" />
    


标签: tomcat ant