Johan Sjöberg
Johan Sjöberg

Reputation: 49187

Jetty maven plugin multiple webapps on different ports

How can I run multiple webapps on different ports using the latest version of the jetty maven plugin?

org.eclipse.jetty:jetty-maven-plugin (version 9.2.2.v20140723 at the time of writing).

E.g.,

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

The official documententation states this under httpConnector

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

Great, so I configure a name but how do I bind that to a contextHandler? This is what I have so far

<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>

This not yet migrated wiki suggests it can be done using the connectorNames property on the WebAppContext, but that's not available anymore.

Upvotes: 2

Views: 3435

Answers (3)

Johan Sj&#246;berg
Johan Sj&#246;berg

Reputation: 49187

Expanding on the answer of @BLuEGoD, here's the complete working configuration

Plugin configuration:

<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>

The jetty.xml configuration

<?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>

Upvotes: 4

BLuEGoD
BLuEGoD

Reputation: 251

Have a look at the documentation:

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

It is also possible to use an extension to the virtual host mechanism with named to connectors to make some web applications only accessible by specific connectors. If a connector has a name "MyConnector" set using the setName method, then this can be referenced with the special virtual host name "@MyConnector".

You can then imply the context as it will contain the virtualhost:

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

Using @ConnectorName:

@ConnectorName A connector name, which is not strictly a virtual host, but instead will only match requests that are received on connectors that have a matching name set with Connector.setName(String).

The configuration in the links above is based on a separate jetty xml configuration file. I haven't tested this but you can possibly insert this into your contextHandler (which has a setter):

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

That should bind with the corresponding connector.

You could also do this programmatically in Java.

Upvotes: 4

isalgueiro
isalgueiro

Reputation: 2033

I'd try to do it using jettyXml parameter instead of connector and contextHandlers. Write an xml config file for each war and reference them in the jettyXml parameter in your pom.

Upvotes: 0

Related Questions