Maven Jetty Plugin
Labels
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery
Jetty7 - This plugin was renamed to jetty-maven-plugin to better conform to maven2 convention.
For rapid application development with webapp overlays, see Multiple WebApp Source Directory
Maven Jetty Plugin Configuration Guide
In order to run Jetty on a webapp project which is structured according to the usual Maven defaults (resources in ${basedir}/src/main/webapp, classes in ${project.build.outputDirectory} and the web.xml descriptor at ${basedir}/src/main/webapp/WEB-INF/web.xml, you don't need to configure anything.
Simply type:
mvn jetty:run
This will start Jetty running on port 8080 and serving your project. Jetty will continue to run until the plugin is explicitly stopped, for example, by a <cntrl-c>. You can also use the mvn jetty:stop command.
It is extremely convenient to leave the plugin running because it can be configured to periodically scan for changes and automatically redeploy the webapp. This makes the development cycle much more productive by eliminating the build and deploy steps: you use your IDE to make changes to the project and the running web container will automatically pick them up, allowing you to test them straight away.
If, for whatever reason, you cannot run on an unassembled webapp, the plugin also supports the jetty:run-war and jetty:run-exploded goals which are discussed below.
More information on each of the goals is available at mvn jetty:run page, mvn jetty:run-exploded page, mvn jetty:run-war page and the Jetty Documentation.
Automatic execution of the plugin
Sometimes, for example when doing integration testing, you'd like to be able to automatically have your webapp started at the beginning of the tests, and stopped at the end rather than manually executing mvn jetty:run on the command line.
To do this, you need to set up a couple of <execution> scenarios for the jetty plugin and use the <daemon>true</daemon> configuration option to prevent jetty running indefinitely and force it to only execute while maven is running.
The pre-integration-test and post-integration-test maven build phases can be used to trigger the execution and termination of jetty like so:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<profile>
...
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
</profile>
How to stop the plugin from the command line
The run, run-war and run-exploded goals leave the plugin running indefinitely. You can terminate it with a <cntrl-c> in the controlling terminal window, or by executing the stop goal in another terminal window. If you wish to be able to use mvn jetty:stop then you need to configure the plugin with a special port number and key that you also supply on the stop command:
Here's an example configuration:
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.10</version> <configuration> <stopPort>9966</stopPort> <stopKey>foo</stopKey> </configuration> </plugin>
To start:
mvn jetty:start
To stop:
mvn jetty:stop
How to configure the plugin
Configuration common to run, run-war and run-exploded goals
Regardless of which jetty goal you execute, the following configuration parameters are common to all. They are divided into configuration that applies to the container as a whole, and configuration that applies specifically to the webapp:
Container Configuration
As of Jetty 6.2.0pre0 a new feature to control webapp redeployment will be available.
The configuration parameter is: <reload>[manual|automatic]</reload>
When set to manual, no automatic scanning and redeployment of the webapp is done. Rather, the user can control when the webapp is reloaded by tapping the carriage return key. Set to automatic the scanning and automatic redeployment is performed at intervals controlled by the scanIntervalSeconds parameter. The choice of reloading paradigm can also be configured on the command line by use of the -Djetty.reload system parameter.
For example: "mvn -Djetty.reload=manual jetty:run" would force manual reloading, regardless of what is configured in the project pom. Similarly: "mvn -Djetty.reload=automatic -Djetty.scanIntervalSeconds=10 jetty:run" will force automatic background reloading with a sweep every 10 seconds, regardless of the configuration in the project pom.
Webapp Configuration
As of release 6.1.6rc0, an alternative and more flexible way to configure the webapp is to use the webAppConfig element instead of the individual parameters listed above. With the webAppConfig element, you can effectively call any of the setter methods on the org.mortbay.jetty.webapp.WebAppContext class, an instance of which represents your webapp. The example below shows how to use this element to configure the same parameters as above (but of course there are many more you can set):
<project> ... <plugins> ... <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <!-- Configure the webapp --> <contextPath>/biggerstrongerbetterfaster</contextPath> <tmpDir>target/not/necessary</tmpDir> <webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml> <overrideWebXml>src/main/resources/override-web.xml</overrideWebXml> <!-- OR, as of jetty6.1.6rc0, you can use the webAppConfig element instead <webAppConfig> <contextPath>/test</contextPath> <tempDirectory>${project.build.directory}/work</tempDirectory> <defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor> <overrideDescriptor>src/main/resources/override-web.xml</overrideDescriptor> </webAppConfig> --> <!-- configure the container --> <jettyConfig>/my/special/jetty.xml</jettyConfig> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>9090</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> <userRealms> <userRealm implementation="org.mortbay.jetty.security.HashUserRealm"> <name>Test Realm</name> <config>etc/realm.properties</config> </userRealm> </userRealms> <requestLog implementation="org.mortbay.jetty.NCSARequestLog"> <filename>target/yyyy_mm_dd.request.log</filename> <retainDays>90</retainDays> <append>true</append> <extended>false</extended> <logTimeZone>GMT</logTimeZone> </requestLog> </configuration> </plugin> </plugins> </project>
Configuration for the jetty:run Goal
The run goal allows you to deploy your unassembled webapp to jetty, based on the locations of its constituent parts in your pom.xml. The following extra configuration parameters are available:
Here's an example of setting all of these parameters:
<project> ... <plugins> ... <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <configuration> <webAppSourceDirectory>${basedir}/src/staticfiles</webAppSourceDirectory> <webXml>${basedir}/src/over/here/web.xml</webXml> <jettyEnvXml>${basedir}/src/over/here/jetty-env.xml</jettyEnvXml> <classesDirectory>${basedir}/somewhere/else</classesDirectory> <scanTargets> <scanTarget>src/mydir</scanTarget> <scanTarget>src/myfile.txt</scanTarget> </scanTargets> <scanTargetPatterns> <scanTargetPattern> <directory>src/other-resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <excludes> <exclude>**/myspecial.xml</exclude> <exclude>**/myspecial.properties</exclude> </excludes> </scanTargetPattern> </scanTargetPatterns> </configuration> </plugin> </plugins> </project>
See also the jetty:run parameter reference.
Configuration for the jetty:run-war Goal
This goal will first package your webapp as a war file and then deploy it to Jetty. If you set a non-zero scanInterval Jetty will watch your pom.xml and the war file and if either changes, it will redeploy the war.
The configuration parameters specific to this goal are:
Here's how you would set it:
<project> ... <plugins> ... <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <configuration> <webApp>${basedir}/target/mycustom.war</webApp> </configuration> </plugin> </plugins> </project>Â Â Â Â Â Â Â
See also the jetty:run-war parameter reference.
Configuration for the jetty:run-exploded Goal
This goal first assembles your webapp into an exploded war file and then deploys it to Jetty. If you set a non-zero scanInterval, Jetty will watch your pom.xml, WEB-INF/lib, WEB-INF/classes and WEB-INF/web.xml for changes and redeploy when necessary.
The configuration parameters specific to this goal are:
Here's how you would set it:
<project> ... <plugins> ... <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <configuration> <webApp>${basedir}/target/myfunkywebapp</webApp> </configuration> </plugin> </plugins> </project>
See also the jetty:run-exploded parameter reference.
Setting System Properties
You may specify property name/value pairs that will be set as System properties for the execution of the plugin. Note that if a System property is found that is already set (eg from the command line or by the JVM itself), then these configured properties DO NOT override them. This feature is useful to tidy up the command line and save a lot of typing. For example, to set up Commons logging you would usually need to type:
mvn -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog jetty:run
Using the systemProperty configuration the command line can again be shorted to mvn jetty:run by placing the following in the pom.xml:
<project> ... <plugins> ... <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <configuration> ... <systemProperties> <systemProperty> <name>org.apache.commons.logging.Log</name> <value>org.apache.commons.logging.impl.SimpleLog</value> </systemProperty> ... </systemProperties> </configuration> </plugin> </plugins> </project>
Note: You can use either <name> or <key> to specify the name of a <systemProperty>. Use whichever you prefer.
Logging
Jetty itself has no dependencies on a particular logging framework, using a built-in logger which outputs to stderr. However, to allow jetty to integrate with other logging mechanisms, if an SLF4J log implementation is detected in the classpath, it will use it in preference to the built-in logger.
The JSP engine used by jetty does however have logging dependencies. If you are using JSP 2.0 (ie you are running in a JVM version < 1.5), the JSP engine depends on commons-logging. A default commons-logging logger will be provided by the plugin using a combination of the jcl04-over-slf4j and the simple-slf4j implementation, which logs all messages INFO level and above. You can override this and provide your own commons-logging delegated logger by following these steps:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.0-SNAPSHOT</version>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
</configuration>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
<version>1.0.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>
mvn -Dslf4j=false jetty:run
Note: if you are using log4j you will need to tell the log4j discovery mechanism where your configuration properties file is. For example:
mvn -Dslf4j=false -Dlog4j.configuration=file:./target/classes/log4j.properties jetty:run
If you are using JSP2.1 (ie you are running in a JVM >= 1.5), then rejoice because the JSP engine has no particular logging dependencies.
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery