Created
May 15, 2012 20:10
-
-
Save JamesMGreene/2704730 to your computer and use it in GitHub Desktop.
How to start and stop Jetty from Ant. Two versions: one using Ant Contrib, one without.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
*********************** | |
A more-correct version of the Ant script bits outlined on this blog post: | |
http://ptrthomas.wordpress.com/2006/10/10/how-to-start-and-stop-jetty-from-ant/ | |
NOTE: This file's macrodefs use task definitions from the Ant Contrib library ("if", "then" and "else", in particular). | |
If you are not using the Ant Contrib library, you cannot use this version of the file. | |
*********************** | |
--> | |
<!-- | |
Macro: jetty-start | |
Description: Starts the Jetty web server in @{host-dir}. Fails if there is already a server running or if the server does not start. | |
Usage: | |
<jetty-start host-dir="${dir.site}" /> | |
--> | |
<macrodef name="jetty-start"> | |
<attribute name="host-dir" /> | |
<attribute name="host-port" default="8080" /> | |
<attribute name="kill-port" default="8079" /> | |
<sequential> | |
<fail message="FAILED! The HTTP server was already running."> | |
<socket server="localhost" port="@{host-port}" /> | |
</fail> | |
<echo level="info">Starting HTTP server on port @{host-port}, hosting the files from @{host-dir}</echo> | |
<java jar="${jetty.home}/start.jar" fork="true" dir="${jetty.home}"> | |
<sysproperty key="file.encoding" value="UTF-8" /> | |
<sysproperty key="STOP.PORT" value="@{kill-port}" /> | |
<sysproperty key="STOP.KEY" value="secret" /> | |
<arg value="@{host-port}"/> | |
<arg value="@{host-dir}"/> | |
</java> | |
<!-- Although we could use the timeoutproperty attribute, doing so would limit the reusability of this macro --> | |
<waitfor maxwait="10" maxwaitunit="second" checkevery="500" checkeveryunit="millisecond"> | |
<socket server="localhost" port="@{host-port}" /> | |
</waitfor> | |
<fail message="FAILED! The HTTP server did not start."> | |
<!-- So instead of using the timeoutproperty attribute on the waitfor task above, we just check one more time here --> | |
<not> | |
<socket server="localhost" port="@{port}" /> | |
</not> | |
</fail> | |
<echo level="info">The HTTP server has been started.</echo> | |
</sequential> | |
</macrodef> | |
<!-- | |
Macro: jetty-stop | |
Description: Stops the Jetty web server, if running. Fails if the server does not stop. | |
Usage: | |
<jetty-stop /> | |
--> | |
<macrodef name="jetty-stop"> | |
<attribute name="host-port" default="8080" /> | |
<attribute name="kill-port" default="8079" /> | |
<sequential> | |
<if> | |
<socket server="localhost" port="@{host-port}" /> | |
<then> | |
<echo level="info">Stopping HTTP server on port @{host-port}, if running</echo> | |
<java jar="${jetty.home}/start.jar" fork="true" dir="${jetty.home}"> | |
<sysproperty key="STOP.PORT" value="@{kill-port}" /> | |
<sysproperty key="STOP.KEY" value="secret" /> | |
<arg value="--stop" /> | |
</java> | |
<!-- Although we could use the timeoutproperty attribute, doing so would limit the reusability of this macro --> | |
<waitfor maxwait="10" maxwaitunit="second" checkevery="500" checkeveryunit="millisecond"> | |
<not> | |
<socket server="localhost" port="@{host-port}" /> | |
</not> | |
</waitfor> | |
<fail message="FAILED! The HTTP server is still running after attempting to stop it."> | |
<!-- So instead of using the timeoutproperty attribute on the waitfor task above, we just check one more time here --> | |
<socket server="localhost" port="@{host-port}" /> | |
</fail> | |
<echo level="info">The HTTP server has been stopped.</echo> | |
</then> | |
<else> | |
<echo level="info">There is no HTTP server currently running to stop.</echo> | |
</else> | |
</if> | |
</sequential> | |
</macrodef> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
*********************** | |
A more-correct version of the Ant script bits outlined on this blog post: | |
http://ptrthomas.wordpress.com/2006/10/10/how-to-start-and-stop-jetty-from-ant/ | |
NOTE: This file's macrodefs do NOT use any task definitions from the Ant Contrib library. | |
*********************** | |
--> | |
<!-- | |
Macro: jetty-start | |
Description: Starts the Jetty web server in @{host-dir}. Fails if there is already a server running or if the server does not start. | |
Usage: | |
<jetty-start host-dir="${dir.site}" /> | |
--> | |
<macrodef name="jetty-start"> | |
<attribute name="host-dir" /> | |
<attribute name="host-port" default="8080" /> | |
<attribute name="kill-port" default="8079" /> | |
<sequential> | |
<fail message="FAILED! The HTTP server was already running."> | |
<socket server="localhost" port="@{host-port}" /> | |
</fail> | |
<echo level="info">Starting HTTP server on port @{host-port}, hosting the files from @{host-dir}</echo> | |
<java jar="${jetty.home}/start.jar" fork="true" dir="${jetty.home}"> | |
<sysproperty key="file.encoding" value="UTF-8" /> | |
<sysproperty key="STOP.PORT" value="@{kill-port}" /> | |
<sysproperty key="STOP.KEY" value="secret" /> | |
<arg value="@{host-port}"/> | |
<arg value="@{host-dir}"/> | |
</java> | |
<!-- Although we could use the timeoutproperty attribute, doing so would limit the reusability of this macro --> | |
<waitfor maxwait="10" maxwaitunit="second" checkevery="500" checkeveryunit="millisecond"> | |
<socket server="localhost" port="@{host-port}" /> | |
</waitfor> | |
<fail message="FAILED! The HTTP server did not start."> | |
<!-- So instead of using the timeoutproperty attribute on the waitfor task above, we just check one more time here --> | |
<not> | |
<socket server="localhost" port="@{port}" /> | |
</not> | |
</fail> | |
<echo level="info">The HTTP server has been started.</echo> | |
</sequential> | |
</macrodef> | |
<!-- | |
Macro: jetty-stop | |
Description: Stops the Jetty web server, if running. Fails if the server does not stop. | |
Usage: | |
<jetty-stop /> | |
--> | |
<macrodef name="jetty-stop"> | |
<attribute name="host-port" default="8080" /> | |
<attribute name="kill-port" default="8079" /> | |
<sequential> | |
<echo level="info">Stopping HTTP server on port @{host-port}, if running</echo> | |
<java jar="${jetty.home}/start.jar" fork="true" dir="${jetty.home}"> | |
<sysproperty key="STOP.PORT" value="@{kill-port}" /> | |
<sysproperty key="STOP.KEY" value="secret" /> | |
<arg value="--stop" /> | |
</java> | |
<!-- Although we could use the timeoutproperty attribute, doing so would limit the reusability of this macro --> | |
<waitfor maxwait="10" maxwaitunit="second" checkevery="500" checkeveryunit="millisecond"> | |
<not> | |
<socket server="localhost" port="@{host-port}" /> | |
</not> | |
</waitfor> | |
<fail message="FAILED! The HTTP server is still running after attempting to stop it."> | |
<!-- So instead of using the timeoutproperty attribute on the waitfor task above, we just check one more time here --> | |
<socket server="localhost" port="@{host-port}" /> | |
</fail> | |
<echo level="info">The HTTP server has been stopped.</echo> | |
</sequential> | |
</macrodef> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment