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"/>
   </path>
   <taskdef classpathref="jetty.cp" resource="tasks.properties" loaderref="jetty.loader" />
</project>

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}">
      <connectors>
         <selectchannelconnector port="80" />
      </connectors>
      <webapp name="Web Application" warfile="${dist.dir}/${dist.dir.name}" contextpath="/">
         <classes dir="./src/resources" includes="logback-test.xml">
         </classes>
      </webapp>
   </jetty>
</target>

4. Run ant target jetty to start web app.

Rate article
ASJAVA.COM
Add a comment

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

*

code

  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 java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    Java Result: 1

    Reply