1. 静态部署:直接把包拷贝到webapp下面,然后启动jetty。当然,目录可以增加的。
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New class="org.mortbay.jetty.webapp.WebAppContext"> <Set name="contextPath">/test</Set> ##指定位置 <Set name="war">demo</Set> ##url的context </New> </Item>
</Array>
</Set>
</New>
</Set>
2. hot deploy:通过context中的配置,指向工程,实现动态部署。当然,也是可以修改的。
<Call name="addLifeCycle"> <Arg> <New class="org.mortbay.jetty.deployer.ContextDeployer"> <Set name="contexts"><Ref id="Contexts"/></Set> <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>##指定目录 <Set name="scanInterval">1</Set> </New> </Arg> </Call>
jetty启动时加入--ini=--ini=start.ini,比如文件内容如下:
etc/jetty.xml etc/jetty-deploy.xml etc/jetty-webapps.xml etc/jetty-contexts.xml etc/jetty-testrealm.xml
jetty-deploy.xml,增加DeploymentManager,handles the lifecycle of deploying web applications
<Configure id="Server" class="org.eclipse.jetty.server.Server"> <Call name="addBean"> <Arg> <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager"> <Set name="contexts"> <Ref id="Contexts" /> </Set> <Call name="setContextAttribute"> <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg> <Arg>.*/jsp-api-[^/]*\.jar$|.*/jsp-[^/]*\.jar{1}lt;/Arg> </Call> </New> </Arg> </Call> </Configure>
jetty-contexts.xml,增加动态部署provider
<Configure id="Server" class="org.eclipse.jetty.server.Server"> <Ref id="DeploymentManager"> <Call name="addAppProvider"> <Arg> <New class="org.eclipse.jetty.deploy.providers.ContextProvider"> <Set name="monitoredDir"><Property name="jetty.home" default="." />/contexts</Set> <Set name="scanInterval">1</Set> </New> </Arg> </Call> </Ref> </Configure>
<Configure id="Server" class="org.eclipse.jetty.server.Server"> <Ref id="DeploymentManager"> <Call name="addAppProvider"> <Arg> <New class="org.eclipse.jetty.deploy.providers.WebAppProvider"> <Set name="monitoredDir"><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">false</Set> </New> </Arg> </Call> </Ref> </Configure>
addBeans方法的作用,在server启动后调用DeploymentManager的生命周期方法
/** * Add an associated bean. * The bean will be added to the servers {@link Container} * and if it is a {@link LifeCycle} instance, it will be * started/stopped along with the Server. Any beans that are also * {@link Destroyable}, will be destroyed with the server. * @param o the bean object to add */
@Override protected void doStart() throws Exception { if (_useStandardBindings) { Log.debug("DeploymentManager using standard bindings"); addLifeCycleBinding(new StandardDeployer()); addLifeCycleBinding(new StandardStarter()); addLifeCycleBinding(new StandardStopper()); addLifeCycleBinding(new StandardUndeployer()); } // Start all of the AppProviders for (AppProvider provider : _providers) { startAppProvider(provider); } super.doStart(); }
private void startAppProvider(AppProvider provider) { try { provider.setDeploymentManager(this); provider.start(); } catch (Exception e) { Log.warn("Unable to start AppProvider",e); } }
scanner负责扫描,内部通过time和timertask完成调度
listener定义扫描之后的操作
filenamefilter定义寻找文件名的规则
如果文件不是目录或者以.war结尾,不处理
if (!file.isDirectory() && !lowername.endsWith(".war")) { return false; }
// is it a directory for an existing war file? if (file.isDirectory() && (new File(dir,name+".war").exists() || new File(dir,name+".WAR").exists())) { return false; }
public boolean accept(File dir, String name) { if (!dir.exists()) return false; String lowername = name.toLowerCase(); return (lowername.endsWith(".xml") && !new File(dir,name).isDirectory()); } }