Ant - Windows Service control

on Thursday, 3 May 2012


When we make any changes to our databases we like to restart the application servers to refresh the datasources. Some of our development environments are run as Windows services and so it would be nice to restart them remotely from the build machines. Here's how with Ant:
<target name="bounce-svc" description="Bounce service on HOST">
<bounceService server="${j2ee.host}" service="beasvc ${weblogic.domain.name}_${weblogic.deploy.target}" />
</target>
<macrodef name="bounceService">
<attribute name="server" />
<attribute name="service" />
<sequential>
<echo message="Bouncing @{service} on @{server}" />
<exec executable="sc" outputproperty="dummy">
<arg line="\\@{server} stop &quot;@{service}&quot;" />
</exec>
<exec executable="sc" outputproperty="dummy">
<arg line="\\@{server} start &quot;@{service}&quot;" />
</exec>
<echo message="Completed bounce of @{service} on @{server}" />
</sequential>
</macrodef>
view raw ant-winsvc.xml hosted with ❤ by GitHub

I hope you can see in this snippet that the code runs 2 tasks - Stop and Start. You can break down the code if you just want a Start or Stop only script.

1 comments:

tanchao said...

Thanks, this helps me with my work.

Post a Comment