Jetty应用部署方式大概有如下几种:
测试环境:JDK1.6,jetty-distribution-8.1.8.v20121106
1,直接将打包后的war文件放到${JETTY_HOME}/webapps,Jetty启动时通过org.eclipse.jetty.deploy.providers.WebAppProvider去将${JETTY_HOME}/webapps里的合法web包找到并部署。当然你也可以将解压后的web包以文件夹的方式部署(在没有修改上下文的默认情况下,上下文是当前文件夹的名称。如文件夹名字是“aaa”,则上下文也为“aaa”,如果文件夹名称是“aaa.war”,则上下文是“aaa.war”,而不是“aaa”)。
2,通过修改${JETTY_HOME}/contents下默认自带的test.xml文件来部署你的应用。当然你可添加一个类似于test.xml文件来部署。
test.xml内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<!-- ==================================================================
Configure and deploy the test web application in $(jetty.home)/webapps/test
Note. If this file did not exist or used a context path other that /test
then the default configuration of jetty.xml would discover the test
webapplication with a WebAppDeployer. By specifying a context in this
directory, additional configuration may be specified and hot deployments
detected.
===================================================================== -->
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- Required minimal context configuration : -->
<!-- + contextPath -->
<!-- + war OR resourceBase -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<Set name="contextPath">/</Set>
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- Optional context configuration -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<Set name="extractWAR">true</Set>
<Set name="copyWebDir">false</Set>
<Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>
<Set name="overrideDescriptor"><SystemProperty name="jetty.home" default="."/>/contexts/test.d/override-web.xml</Set>
<!-- virtual hosts
<Set name="virtualHosts">
<Array type="String">
<Item>www.myVirtualDomain.com</Item>
<Item>localhost</Item>
<Item>127.0.0.1</Item>
</Array>
</Set>
-->
<!-- disable cookies
<Get name="sessionHandler">
<Get name="sessionManager">
<Set name="usingCookies" type="boolean">false</Set>
</Get>
</Get>
-->
<Get name="securityHandler">
<Set name="loginService">
<New class="org.eclipse.jetty.security.HashLoginService">
<Set name="name">Test Realm</Set>
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
<!-- To enable reload of realm when properties change, uncomment the following lines -->
<!-- changing refreshInterval (in seconds) as desired -->
<!--
<Set name="refreshInterval">5</Set>
<Call name="start"></Call>
-->
</New>
</Set>
<Set name="authenticator">
<New class="org.eclipse.jetty.security.authentication.FormAuthenticator">
<Set name="alwaysSaveUri">true</Set>
</New>
</Set>
<Set name="checkWelcomeFiles">true</Set>
</Get>
<!-- Non standard error page mapping -->
<!--
<Get name="errorHandler">
<Call name="addErrorPage">
<Arg type="int">500</Arg>
<Arg type="int">599</Arg>
<Arg type="String">/dump/errorCodeRangeMapping</Arg>
</Call>
</Get>
-->
<!-- Add context specific logger
<Set name="handler">
<New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler">
<Set name="requestLog">
<New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog">
<Set name="filename"><Property name="jetty.logs" default="./logs"/>/test-yyyy_mm_dd.request.log</Set>
<Set name="filenameDateFormat">yyyy_MM_dd</Set>
<Set name="append">true</Set>
<Set name="LogTimeZone">GMT</Set>
</New>
</Set>
</New>
</Set>
-->
</Configure>
其中<Set name="contextPath">/aaa</Set>指应用上下文,而 <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set>则指web包所在地,它可以指向一个任意非Jetty下的web文件夹。如:<Set name="war">E:/workspace/demo/src/main/webapp</Set>
这两种方式的部署在${JETTY_HOME}/etc/jetty-webapps.xml中都有定义 :
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<!-- =============================================================== -->
<!-- Add a WebAppProvider to the deployment manager -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- This scans the webapps directory for war files and directories -->
<!-- to deploy. -->
<!-- This configuration must be used with jetty-deploy.xml, which -->
<!-- creates the deployment manager instance -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Ref id="DeploymentManager">
<Call id="webappprovider" name="addAppProvider">
<Arg>
<New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
<Set name="monitoredDirName"><Property name="jetty.home" default="." />/webapps</Set>
<Set name="defaultsDescriptor"><Property name="jetty.home" default="."/>/etc/webdefault.xml</Set>
<Set name="scanInterval">1</Set>
<Set name="contextXmlDir"><Property name="jetty.home" default="." />/contexts</Set>
<Set name="extractWars">true</Set>
</New>
</Arg>
</Call>
</Ref>
</Configure>
从这里可以看到,webapps和contexts的路径都是可以自定义的,但不推荐,惯例编程才是最好的!
下面再扩展几种方式,虽然看起来不太一样,但实际上都是一样的,只是有的采用程序代码来描述部署信息,有的采用工具生成部署信息。
3,Java程序部署Jetty应用。
package com.jerval.demo;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.webapp.WebAppContext;
public class Main {
public static void main(String[] args) throws Exception {
Server server = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(9080);
server.setConnectors(new Connector[] {connector});
WebAppContext context = new WebAppContext("E:/workspace/aaa/WebContent", "/aaa");
server.setHandler(context);
server.setStopAtShutdown(true);
server.setSendServerVersion(true);
server.start();
server.join();
}
}
pomxml:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.10.v20130312</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.1.10.v20130312</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.10.v20130312</version>
</dependency>
4,通过Jetty Eclipse插件run-jetty-run来部署应用并启动Jetty。
插件下载地址:http://run-jetty-run.googlecode.com/svn/trunk/updatesite 。完成后当你在Eclipse里的web应用上右键“Run As”时,你会看到“Run Jetty”,点击即可运行Jetty。你也可以通过“Run Configuration...”来修改Jetty默认配置。
5,通过Maven插件来部署应用并启动Jetty:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.5.v20120716</version>
<configuration>
<stopPort>9966</stopPort>
<stopKey>foo</stopKey>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<webApp>
<contextPath>/aaa</contextPath>
</webApp>
</configuration>
</plugin>
然后使用命令:mvn jetty:run来完成Maven打包部署应用并启动Jetty。配置中提供了修改端口的方式,当然你也可以通过命令来修改:mvn -Djetty.port=9080 jetty:run 。
6,通过Jetty WTP Plugin来部署应用并启动Jetty:
下载Jetty WTP Plugin:http://download.eclipse.org/jetty/updates/jetty-wtp. 通过Eclipse的添加插件方式来安装.更请参考http://wiki.eclipse.org/Jetty_WTP_Plugin/Jetty_WTP_Install |