How do I Start and Stop Jetty Server from Ant

To start and stop Jetty from Ant command line, we can use the Ant Jetty plugin jetty-ant in the codehouse trunk, it is a new plugin available in the /extras/ant location, which makes it possible to start Jetty web server directly from the Ant build script, and, actually, to embed the Jetty web server inside your build process.

1. Please download and install Jetty and its extras, if jetty-ant jar does not exist get it jetty-ant jar, copy it to either project or ant lib folder.

2. Create a Ant build XML, define a task jettytaskdef exports jetty task available from the loading classes(it is not a standard task).

<project name="Start jetty from ant" basedir=".">
   <path id="jetty.cp">
      <fileset dir="jetty-lib" includes="*.jar"/>
   <taskdef classpathref="jetty.cp" resource="" loaderref="jetty.loader" />

3. Add a new target ‘jetty’, create a folder ${jetty.temp} which specify where temporary files are stored, then and extra tags webapp and connectors inside jetty tag.

connectors – defines Jetty connectors (what’s channel connector).

webapp – a particular web application will be deployed to.

<target name="jetty" depends="init">
   <mkdir dir="logs"/><!-- this must be in the working directory and must exist -->
   <typedef name="selectchannelconnector" classname="org.mortbay.jetty.nio.SelectChannelConnector"
classpathref="jetty.cp" loaderref="jetty.loader"/>
   <mkdir dir="${jetty.temp}"/>
   <jetty tempDirectory="${jetty.temp}">
         <selectchannelconnector port="80" />
      <webapp name="Web Application" warfile="${dist.dir}/${}" contextpath="/">
         <classes dir="./src/resources" includes="logback-test.xml">

4. Run ant target jetty to start web app.

Оцените статью
Добавить комментарий

Your email address will not be published. Required fields are marked *



  1. Tim

    I have tried this and several other variations to start jetty 7.1.4 from ant in a NetBeans build.xml. It works fine from a command line. From ant I always get:

    Exception in thread “main” java.lang.NoClassDefFoundError:
    Caused by: java.lang.ClassNotFoundException:
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    Java Result: 1