2014-08-31 46 views
2

如何使用最新版本的jetty maven插件在不同端口上運行多個webapps?Jetty maven插件不同端口上的多個webapps

org.eclipse.jetty:jetty-maven-plugin(撰寫本文時爲9.2.2.v20140723)。

httpConnector

name: 
    The name of the connector, which is useful for configuring contexts to 
    respond only on particular connectors. 

大下

例如,

foo.war -> localhost:8080/ 
bar.war -> localhost:8081/ 
baz.war -> localhost:8082/ 

The official documententation狀態這一點,所以我配置name但我怎麼綁定是一個contextHandler?這是我迄今爲止

<plugin> 
    <groupId>org.eclipse.jetty</groupId> 
    <artifactId>jetty-maven-plugin</artifactId> 
    <version>9.2.2.v20140723</version> 
    <configuration> 
    <connectors> 
     <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> 
     <port>8080</port> 
     <name>instance_8080</name> 
     </connector> 
     <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> 
     <port>8081</port> 
     <name>instance_8081</name> 
     </connector> 
    </connectors> 
    <contextHandlers>   
     <contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext"> 
     <war>a.war</war> 
     <contextPath>/</contextPath> 
    </contextHandler> 
    <contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext"> 
     <war>b.war</war> 
     <contextPath>/</contextPath> 
    </contextHandler> 
    </contextHandlers> 
</plugin> 

not yet migrated wiki表明,它可以用在WebAppContextconnectorNames屬性來完成,但是這不再可用。

+0

的'<連接器執行=「org.eclipse.jetty.server.nio.SelectChannelConnector」>'無效Jetty 9.2.2(該類在Jetty 9中不存在) – 2014-09-30 23:03:04

+0

@JoakimErdfelt,評論很好。下面的答案包含一個工作示例,唯一的缺點是它需要'jetty.xml'。 – 2014-10-01 07:12:57

+0

是的,[I](http://projects.eclipse.org/content/joakim-erdfelt-committer-jetty-servlet-engine-and-http-server)完全知道這一點:) – 2014-10-01 12:46:39

回答

3

看一看文檔:

http://www.eclipse.org/jetty/documentation/current/serving-webapp-from-particular-port.html

也可以使用擴展到虛擬主機機制命名的連接器,使一些Web應用程序只能通過特定的連接器訪問。如果連接器具有使用setName方法設置的名稱「MyConnector」,則可以使用特殊的虛擬主機名稱「@MyConnector」來引用該名稱。

然後,您可以暗示上下文它將包含虛擬主機:

http://www.eclipse.org/jetty/documentation/current/configuring-virtual-hosts.html#different-virtual-hosts-different-contexts

使用@ConnectorName:

@ConnectorName 連接器名稱,這不是嚴格意義上的虛擬主機,而只會匹配在具有與Connector.setName(String)一起設置的匹配名稱的連接器上接收到的請求。

上述鏈接中的配置基於單獨的jetty xml配置文件。我沒有測試過這個,但是你可以把它插入你的contextHandler(它有一個setter):

<virtualHosts> 
    <virtualHost>@instance_8080</virtualHost> 
</virtualHosts> 

這應該與相應的連接器綁定。

你也可以用Java編程。

+0

它的工作原理如果一個' jetty.xml'(在插件配置中不支持'virtualHosts') – 2014-09-13 19:26:48

0

我會嘗試使用jettyXml參數來代替connectorcontextHandlers。爲每場戰爭寫一個xml config file,並在你的pom的jettyXml參數中引用它們。

4

擴大對@BLuEGoD的答案,以下是完整的工作配置

插件配置:

<plugin> 
    <groupId>org.eclipse.jetty</groupId> 
    <artifactId>jetty-maven-plugin</artifactId> 
    <version>9.2.2.v20140723</version> 
    <configuration> 
     <jettyXml>path/to/jetty.xml</jettyXml> 
    </configuration> 
</plugin> 

的配置的jetty.xml

<?xml version="1.0"?> 
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> 
<Configure id="Server" class="org.eclipse.jetty.server.Server"> 
<Set name="connectors"> 
    <Array type="org.eclipse.jetty.server.Connector"> 
     <Item> 
      <New class="org.eclipse.jetty.server.ServerConnector"> 
       <Arg> <Ref refid="Server"/> </Arg> 
       <Set name="port">8080</Set> 
       <Set name="name">instance_8080</Set> 
      </New> 
     </Item> 
     <Item> 
      <New class="org.eclipse.jetty.server.ServerConnector"> 
       <Arg> <Ref refid="Server"/> </Arg> 
       <Set name="port">8081</Set> 
       <Set name="name">instance_8081</Set> 
      </New> 
     </Item> 
    </Array> 
</Set> 

<New id="context-foo" class="org.eclipse.jetty.maven.plugin.JettyWebAppContext"> 
    <Set name="contextPath">/</Set> 
    <Set name="war">foo.war</Set> 
    <Set name="virtualHosts"> 
     <Array type="java.lang.String"> 
      <Item>@instance_8080</Item> 
     </Array> 
    </Set> 
</New> 

<New id="context-bar" class="org.eclipse.jetty.maven.plugin.JettyWebAppContext"> 
    <Set name="contextPath">/</Set> 
    <Set name="war">bar.war</Set> 
    <Set name="virtualHosts"> 
     <Array type="java.lang.String"> 
      <Item>@instance_8081</Item> 
     </Array> 
    </Set> 
</New> 

<Set name="handler"> 
    <New class="org.eclipse.jetty.server.handler.ContextHandlerCollection"> 
     <Set name="handlers"> 
      <Array type="org.eclipse.jetty.server.Handler"> 
       <Item> 
        <Ref refid="context-foo" /> 
       </Item> 
       <Item> 
        <Ref refid="context-bar" /> 
       </Item> 
       <Item> 
        <New class="org.eclipse.jetty.server.handler.DefaultHandler" /> 
       </Item> 
      </Array> 
     </Set> 
    </New> 
</Set> 

+0

這些是裸體的'ServerConnectors'(即沒有關聯的'HttpConfiguration') – 2014-10-01 12:43:37

+0

@Johan Sjoberg。感謝您的回答。我嘗試過這個。它適用於一個webAppContext。我有兩個webAppContext具有不同的上下文路徑,但端口相同。當我運行時,構建失敗,因爲拋出異常說,端口已被使用。我如何配置具有相同的端口,但不同的contexPaths? – 2016-05-01 12:14:19

相關問題