Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Jetty/Howto/Use Jetty with Ant"

< Jetty‎ | Howto
m
m
Line 12: Line 12:
  
 
# Get the latest jetty sources from http://dist.codehaus.org/jetty/
 
# Get the latest jetty sources from http://dist.codehaus.org/jetty/
# Unpack jetty and build it with 'mvn install' (If you don't have Maven yet, get it from http://maven.apache.org/)
+
# Unpack jetty and build it with <tt>mvn install</tt> (If you don't have Maven yet, get it from http://maven.apache.org/)
 
# Go to the extras/ant/ folder and build the ant plugin with 'mvn install'
 
# Go to the extras/ant/ folder and build the ant plugin with 'mvn install'
 
# Go to <your M2 repo root>/repository/org/eclipse/jetty/jetty-ant and get the jetty-ant plugin JAR.
 
# Go to <your M2 repo root>/repository/org/eclipse/jetty/jetty-ant and get the jetty-ant plugin JAR.
Line 28: Line 28:
 
==Preparing the <tt>build.xml file</tt>==
 
==Preparing the <tt>build.xml file</tt>==
  
Let's start with an empty build.xml  
+
Let's start with an empty build.xml:
  
 
<source lang="xml">
 
<source lang="xml">
Line 35: Line 35:
 
</source>
 
</source>
  
Then, you must define a ''jetty'' task:  
+
Then, you must define a <tt>jetty</tt> task:  
  
 
<source lang="xml">
 
<source lang="xml">
Line 61: Line 61:
 
</source>
 
</source>
  
Starting jetty on port 8080 is then a simple matter of executing ''ant jetty.run'' from the command line.
+
Starting jetty on port 8080 is then a simple matter of executing <tt>ant jetty.run</tt> from the command line.
  
 
==Deploying web applications==
 
==Deploying web applications==
  
Adding web applications to the Jetty web server is now a simple matter of putting extra ''webapp'' tags inside ''jetty'' tag. Notice, that a <tt>tempDirectory''</tt> attribute has been added to the '<tt>jetty'</tt> task to specify where temporary files are stored.  
+
To add web applications to the Jetty web server, now put extra <tt>webapp</tt> tags inside <tt>jetty</tt> tag. Notice, that a <tt>tempDirectory''</tt> attribute has been added to the '<tt>jetty'</tt> task to specify where temporary files are stored.  
  
 
<source lang="xml">  
 
<source lang="xml">  
Line 82: Line 82:
 
</source>
 
</source>
  
You can put many ''webapp'' tags inside ''jetty'' tag, thus deploying many web applications simultaneously.
+
You can put many <tt>webapp</tt> tags inside ''jetty'' tag, thus deploying many web applications simultaneously.
  
 
==Plugin syntax==
 
==Plugin syntax==
  
====There are many ways to configure ''jetty'' tasks:====
+
====There are many ways to configure <tt>jetty</tt> tasks:====
  
* ''tempDirectory'' - specifies the Jetty temporary web directory:  
+
* '<tt>tempDirectory</tt>–specifies the Jetty temporary web directory:  
 
<source lang="xml">
 
<source lang="xml">
 
  <jetty tempDirectory="jetty-temp" />
 
  <jetty tempDirectory="jetty-temp" />
 
</source>
 
</source>
* ''requestLog'' - defines a request logger:  
+
* <tt>requestLog</tt>–defines a request logger:  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ... requestLog="your.fancy.request.logger"/>
 
  <jetty ... requestLog="your.fancy.request.logger"/>
 
</source>
 
</source>
* ''jettyXml'' - applies additional configuration via a standard jetty.xml file:  
+
* <tt>jettyXml</tt>–applies additional configuration via a standard jetty.xml file:  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ... jettyXml="path/to/jetty.xml"/>
 
  <jetty ... jettyXml="path/to/jetty.xml"/>
 
</source>
 
</source>
* ''systemProperties'' - adds system properties when starting Jetty:  
+
* <tt>systemProperties</tt>–adds system properties when starting Jetty:  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ...>
 
  <jetty ...>
Line 108: Line 108:
 
  </jetty>
 
  </jetty>
 
</source>
 
</source>
* ''connectors'' - defines Jetty connectors:  
+
* <tt>connectors</tt>–defines Jetty connectors:  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <typedef name="selectChannelConnector" classname="org.eclipse.jetty.nio.SelectChannelConnector"
 
  <typedef name="selectChannelConnector" classname="org.eclipse.jetty.nio.SelectChannelConnector"
Line 119: Line 119:
 
  </jetty>
 
  </jetty>
 
</source>  
 
</source>  
* ''userRealms'' - adds authentication via user "realms":  
+
* <tt>userRealms</tt> - adds authentication via user "realms":  
 
<source lang="xml">>  
 
<source lang="xml">>  
 
  <typedef name="jdbcRealm" classname="org.eclipse.jetty.security.HashUserRealm"
 
  <typedef name="jdbcRealm" classname="org.eclipse.jetty.security.HashUserRealm"
Line 133: Line 133:
 
====There are also many ways to configure web applications with ''webapp'' tag:====
 
====There are also many ways to configure web applications with ''webapp'' tag:====
  
* ''name'' - name of a web application, which is used by DefaultServlet to display application description  
+
* <tt>name</tt>–name of a web application, which is used by DefaultServlet to display application description  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ...>
 
  <jetty ...>
Line 139: Line 139:
 
  </jetty>
 
  </jetty>
 
</source>
 
</source>
* ''warfile'' - path to .war file or a directory with web application contents:  
+
* <tt>warfile</tt>–path to .war file or a directory with web application contents:  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ...>
 
  <jetty ...>
Line 145: Line 145:
 
  </jetty>
 
  </jetty>
 
</source>
 
</source>
* ''contextPath'' - a context path a particular web application will be deployed to, and thus where it will be accessible (<tt>http://localhost:8080/webapp</tt> in this case):  
+
* <tt>contextPath</tt>–a context path a particular web application will be deployed to, and thus where it will be accessible (<tt>http://localhost:8080/webapp</tt> in this case):  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ...>
 
  <jetty ...>
Line 151: Line 151:
 
  </jetty>
 
  </jetty>
 
</source>
 
</source>
* ''jettyEnvXml'' - path to [http://docs.codehaus.org/display/JETTY/jetty-env.xml jettyEnv.xml file with JNDI resource declarations]
+
* <tt>jettyEnvXml</tt>–path to [http://docs.codehaus.org/display/JETTY/jetty-env.xml jettyEnv.xml file with JNDI resource declarations]
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ...>
 
  <jetty ...>
Line 157: Line 157:
 
  </jetty>
 
  </jetty>
 
</source>
 
</source>
* ''webXml'' - path to web.xml file. If not specified, the default (location-of-war-file/WEB-INF/web.xml) location is used:  
+
* <tt>webXml</tt>–path to web.xml file. If not specified, the default (location-of-war-file/WEB-INF/web.xml) location is used:  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ...>
 
  <jetty ...>
Line 163: Line 163:
 
  </jetty>
 
  </jetty>
 
</source>
 
</source>
* ''scanTargets'' and ''scanIntervalSeconds'' - points to special files/folders that need to be scanned, and specifies scanner interval in seconds. If any of the files changes, the web application is automatically reloaded. You can adjust interval and scanned files per web application.
+
* '<tt>scanTargets</tt> and '<tt>scanIntervalSeconds'</tt>–points to special files/folders that need to be scanned, and specifies scanner interval in seconds. If any of the files changes, the web application is automatically reloaded. You can adjust interval and scanned files per web application.
  
{{note|All files included with <classes/> or <lib/> tags are'''automatically''' scanned for changes if you provide ''scanIntervalSeconds'' parameter in the <jetty/> tag. There's no need to add them using the <scanTargets/> section.}}
+
{{note|All files included with <classes/> or <lib/> tags are''automatically'' scanned for changes if you provide <tt>scanIntervalSeconds</tt> parameter in the <tt><jetty/></tt> tag. There's no need to add them using the <tt><scanTargets/></tt> section.}}
 
<source lang="xml">
 
<source lang="xml">
 
  <jetty ...>
 
  <jetty ...>
Line 175: Line 175:
 
  </jetty>
 
  </jetty>
 
</source>
 
</source>
* ''lib'' - specifies which libraries needs to be included for a particular web application (treated as files inside WEB-INF/lib folder). Multiple ''lib'' tags are allowed.  
+
* <tt>lib</tt>–specifies which libraries need be included for a particular web application (treated as files inside WEB-INF/lib folder). Multiple <tt>lib</tt> tags are allowed.  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ...>
 
  <jetty ...>
Line 183: Line 183:
 
  </jetty>
 
  </jetty>
 
</source>
 
</source>
* ''classes'' - specifies which folder contains web application classes (treated as WEB-INF/classes folder). Multiple ''classes'' tags are allowed.  
+
* <tt>classes</tt>–specifies which folder contains web application classes (treated as WEB-INF/classes folder). Multiple ''classes'' tags are allowed.  
 
<source lang="xml">  
 
<source lang="xml">  
 
  <jetty ...>
 
  <jetty ...>
Line 191: Line 191:
 
  </jetty>
 
  </jetty>
 
</source>
 
</source>
* ''connectors'' - specifies additional web application connectors
+
* '<tt>connectors</tt>–specifies additional web application connectors

Revision as of 12:14, 22 September 2010



Introduction

The Ant Jetty plugin is available in the /extras/ant location. This plugin makes it possible to start a Jetty web server directly from the Ant build script, and, to embed the Jetty web server inside your build process. Its purpose is to provide almost the same functionality as the Jetty plugin for Maven: dynamic application reloading, working directly on web application sources, and a tight integration with the build system.

A demo for the Ant Jetty plugin is provided in jetty-contrib repository. It's good to compile Jetty before running these tests, because the demo depends on Jetty JARs from the Maven repository. Alternatively, you can download these JARs manually from one of the Maven repositories. Please see build.xml file in jetty-ant-demo folder for a list of required JARs.

Modifying build.xml from jetty-ant-demo is also the easiest way to start playing with this plugin.

Starting Ant

The Ant plugin depends on Jetty JARs, and so far it's necessary to build Jetty and then the Ant plugin directly from sources. Here's how it goes:

  1. Get the latest jetty sources from http://dist.codehaus.org/jetty/
  2. Unpack jetty and build it with mvn install (If you don't have Maven yet, get it from http://maven.apache.org/)
  3. Go to the extras/ant/ folder and build the ant plugin with 'mvn install'
  4. Go to <your M2 repo root>/repository/org/eclipse/jetty/jetty-ant and get the jetty-ant plugin JAR.
  5. Get these files to run Jetty with JSP/Servlets support (find these in <your M2 repo root>/repository/org/eclipse/jetty):
    • jetty.jar
    • jetty-util.jar
    • jetty-plus.jar
    • jetty-naming.jar
    • servlet-api-2.5.jar
    • jsp-api-2.1.jar
    • jsp-2.1.jar
    • and core-3.1.1.jar from <M2 repository>/org/eclipse/jdt/core/3.1.1/
  6. Put all the JARs inside one lib directory (let's say jetty-lib).

Preparing the build.xml file

Let's start with an empty build.xml:

<project name="Jetty-Ant integration test" basedir=".">
</project>

Then, you must define a jetty task:

<project name="Jetty-Ant integration test" basedir=".">
  <path id="jetty.plugin.classpath">
     <fileset dir="jetty-lib" includes="*.jar"/>
  </path>
  <taskdef classpathref="jetty.plugin.classpath" resource="tasks.properties" loaderref="jetty.loader" />
</project>

Now you are ready to add a new target with Jetty container declaration:

<project name="Jetty-Ant integration test" basedir=".">
  <path id="jetty.plugin.classpath">
    <fileset dir="jetty-lib" includes="*.jar"/>
  </path>
  <taskdef classpathref="jetty.plugin.classpath" resource="tasks.properties" loaderref="jetty.loader" />
 
  <target name="jetty.run">
    <jetty />
  </target>
</project>

Starting jetty on port 8080 is then a simple matter of executing ant jetty.run from the command line.

Deploying web applications

To add web applications to the Jetty web server, now put extra webapp tags inside jetty tag. Notice, that a tempDirectory attribute has been added to the 'jetty' task to specify where temporary files are stored.

 
<project name="Jetty-Ant integration test" basedir=".">
  <path id="jetty.plugin.classpath">
    <fileset dir="jetty-lib" includes="*.jar"/>
  </path>
  <taskdef classpathref="jetty.plugin.classpath" resource="tasks.properties" loaderref="jetty.loader" />
 
  <target name="jetty.run">
    <jetty tempDirectory="jetty-temp">
      <webApp name="webapp1" warfile="webapp1.war" contextpath="/webapp1" />
    </jetty>
  </target>
</project>

You can put many webapp tags inside jetty tag, thus deploying many web applications simultaneously.

Plugin syntax

There are many ways to configure jetty tasks:

  • 'tempDirectory–specifies the Jetty temporary web directory:
 <jetty tempDirectory="jetty-temp" />
  • requestLog–defines a request logger:
 
 <jetty ... requestLog="your.fancy.request.logger"/>
  • jettyXml–applies additional configuration via a standard jetty.xml file:
 
 <jetty ... jettyXml="path/to/jetty.xml"/>
  • systemProperties–adds system properties when starting Jetty:
 
 <jetty ...>
   <systemProperties>
     <systemProperty name="jetty.status" value="Jetty Rocks!"/>
   </systemProperties>
 </jetty>
  • connectors–defines Jetty connectors:
 
 <typedef name="selectChannelConnector" classname="org.eclipse.jetty.nio.SelectChannelConnector"
                            classpathref="jetty.plugin.classpath" loaderref="jetty.loader" />
 ...
 <jetty ...>
   <connectors>
     <selectChannelConnector port="8090" />
   </connectors>
 </jetty>
  • userRealms - adds authentication via user "realms":
> 
 <typedef name="jdbcRealm" classname="org.eclipse.jetty.security.HashUserRealm"
                  classpathref="jetty.plugin.classpath" loaderref="jetty.loader" />
 ...
 <jetty ...>
   <userRealms>
     <userRealm name="realm1" config="path/to/realm/config" />
   </userRealms>
 </jetty>

There are also many ways to configure web applications with webapp tag:

  • name–name of a web application, which is used by DefaultServlet to display application description
 
 <jetty ...>
   <webapp name="name" .../>
 </jetty>
  • warfile–path to .war file or a directory with web application contents:
 
 <jetty ...>
   <webapp warfile="/path/to/war/file/or/web/application/contents" .../>
 </jetty>
  • contextPath–a context path a particular web application will be deployed to, and thus where it will be accessible (http://localhost:8080/webapp in this case):
 
 <jetty ...>
   <webapp contextPath="/webapp" .../>
 </jetty>
 
 <jetty ...>
   <webapp jettyEnvXml="/path/to/jettyEnv.xml" .../>
 </jetty>
  • webXml–path to web.xml file. If not specified, the default (location-of-war-file/WEB-INF/web.xml) location is used:
 
 <jetty ...>
   <webapp webXmlFile="/path/to/web/xml" .../>
 </jetty>
  • 'scanTargets and 'scanIntervalSeconds'–points to special files/folders that need to be scanned, and specifies scanner interval in seconds. If any of the files changes, the web application is automatically reloaded. You can adjust interval and scanned files per web application.
Note.png
All files included with <classes/> or <lib/> tags areautomatically scanned for changes if you provide scanIntervalSeconds parameter in the <jetty/> tag. There's no need to add them using the <scanTargets/> section.
 <jetty ...>
   <webapp ... scanIntervalSeconds="5">
     <scanTargets dir="path/to/included/resources">
       <include name="filename/patterns" />
     </scanTargets>
   </webapp>
 </jetty>
  • lib–specifies which libraries need be included for a particular web application (treated as files inside WEB-INF/lib folder). Multiple lib tags are allowed.
 
 <jetty ...>
   <webapp ...>
     <lib dir="path/to/jar/folder" includes="*.jar" excludes="not-needed.jar" />
   </webapp>
 </jetty>
  • classes–specifies which folder contains web application classes (treated as WEB-INF/classes folder). Multiple classes tags are allowed.
 
 <jetty ...>
   <webapp ...>
     <classes dir="path/to/classes/folder" includes="*.class" />
   </webapp>
 </jetty>
  • 'connectors–specifies additional web application connectors

Back to the top