Search code examples
javajettyembedded-jettymaven-jetty-pluginjetty-9

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.


Solution

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