在码头部署静态内容

Deploy Static content in jetty

我正在尝试在 Jetty 中静态部署应用程序,但使用的是 xml 配置文件,因为我使用的是虚拟主机。

我创建这个文件 xml 用于部署:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" 
  "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/mail</Set>
    <!--<Call name="setInitParameter">
        <Arg>org.eclipse.jetty.servlet.Default.useFileMappedBuffer</Arg>
        <Arg>false</Arg>
    </Call> -->
    <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="resourceBase">/ccmail</Set>
      <Set name="directoriesListed">true</Set>
    </New>
  </Set>

    <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>apps.cairunet.ad.br</Item>     
    </Array>
  </Set> 

</Configure>

我的文件夹应用程序的名称是 ccmail。位于 webapps/ccmail 我已经尝试传递给以下路径:

<Set name="resourceBase">/ccmail</Set>

<Set name="resourceBase">ccmail</Set>

<Set name="resourceBase">./ccmail</Set>

<Set name="resourceBase">webapps/ccmail</Set>

<Set name="resourceBase">/webapps/ccmail</Set>

任何这条路都不适合我。

Jetty 启动报错:

2019-02-25 09:36:46.422:WARN:oejs.ServletContextHandler:main: ServletContextHandler.setHandler should not be called dire ctly. Use insertHandler or setSessionHandler etc. 2019-02-25 09:36:46.484:WARN:oejw.WebInfConfiguration:main: Can't generate resourceBase as part of webapp tmp dir name: java.lang.IllegalStateException: No resourceBase or war set for context 2019-02-25 09:36:46.500:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@7d0587f1{/mail,nul

l,UNAVAILABLE,apps.cairunet.ad.br} java.lang.IllegalStateException: No resourceBase or war set for context at org.eclipse.jetty.webapp.WebInfConfiguration.unpack(WebInfConfiguration.java:577) at org.eclipse.jetty.webapp.WebInfConfiguration.preConfigure(WebInfConfiguration.java:152) at org.eclipse.jetty.webapp.WebAppContext.preConfigure(WebAppContext.java:506) at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:544) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:46) at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:192) at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:505) at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:151) at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180) at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:453) at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:64) at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:610) at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:529) at org.eclipse.jetty.util.Scanner.scan(Scanner.java:392) at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:313) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:150) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:579) at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:240) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138) at org.eclipse.jetty.server.Server.start(Server.java:415) at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117) at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113) at org.eclipse.jetty.server.Server.doStart(Server.java:382) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.xml.XmlConfiguration.run(XmlConfiguration.java:1572) at org.eclipse.jetty.xml.XmlConfiguration.run(XmlConfiguration.java:1512) at java.security.AccessController.doPrivileged(Native Method) at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1511) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.jetty.start.Main.invokeMain(Main.java:220) at org.eclipse.jetty.start.Main.start(Main.java:490) at org.eclipse.jetty.start.Main.main(Main.java:77) 2019-02-25 09:36:46.578:INFO:oejs.AbstractConnector:main: Started ServerConnector@27808f31{HTTP/1.1,[http/1.1]}{0.0.0.0: 8080} 2019-02-25 09:36:46.594:INFO:oejs.Server:main: Started @2970ms

Note: be careful with your DOCTYPE, what you have declared is from Jetty 7.x thru Jetty 8.x, and is not correct for Jetty 9.x

不要混用 ResourceHandler 和 WebAppContext / ServletContextHandler。

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
   "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/mail</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>apps.cairunet.ad.br</Item>     
    </Array>
  </Set> 
</Configure>

最基本的支持是不要在你的<Configure>中引用/ccmail

它以 ${jetty.base}/webapps/ccmail/ 的形式存在就足够了,它将 /ccmail 部署为您的静态资源库。

但是如果你想将静态资源与虚拟主机结合起来,那么你可以使用具有备用基础的 WebAppContext,或者使用新的 ResourceHandler。

备用碱基示例: Serving static files from alternate path in embedded Jetty

ResourceHandler 用法示例: https://www.eclipse.org/jetty/documentation/current/static-content-deployment.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
   "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/ccmail</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="resourceBase">/fully/qualified/path/to/my/jetty.base/webapps/ccmail</Set>
      <Set name="directoriesListed">true</Set>
    </New>
  </Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>apps.cairunet.ad.br</Item>     
    </Array>
  </Set> 
</Configure>