Controlling Jetty port in Maven 2

Overriding the jetty port used on mvn jetty:run was always something that annoyed me…
and that was due to my solution which had been to alter the project’s pom adding:

 	<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
 		<port>9090</port>
 		<maxIdleTime>60000</maxIdleTime>
 	</connector>

One problem with this is that i only need this on one of my dev machines (where the default 8080 port is
already occupied) and additionally this is not something that one would commit and enforce it to others.
It’s just plain wrong.

Maven Profiles

So, one might think, let’s use maven’s profiles for this. In the ~/.m2/settings.xml of the machine
in question, we can add

 <profile>
      <id>override-jetty-port</id>

       <activation>
         <activeByDefault>true</activeByDefault>
        </activation>

 	<!-- something to change the port here -->

 </profile>

But the problem is that it’s not easy to figure out the ‘something to change the port here’ part.
Jetty’s docs talk about the jetty.port property but it must be a system property, i.e. adding

 	<properties>
 		<jetty.port>8084</jetty.port>
 	</properties>

doesn’t do the trick.

Custom Hack

So, i first thought, let’s modify our pom, making use of a custom {jetty.port} in the connector’s implementation,
that let’s define a default value of 8080 for it inside the pom, and then let’s override this in our settings.xml !

But of course, it doesn’t feel right + i’d have to modify too many poms to go unnoticed by others…

Enlightment

And then it hit me… Use the MAVEN_OPTS environment parameter
export MAVEN_OPTS="-Xmx768m -Xms64m -Djetty.port=9090"

is all that it takes… Aargggggg…

Sometimes, things are impossibly easy!

andyhot

4 thoughts on “Controlling Jetty port in Maven 2

  1. Here is the Maven profile you need:

        <profile>
          <id>jetty-alternateport</id>
          <build>
            <plugins>
              <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty-version}</version>
                <configuration>
                  <connectors>
                    <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                      <port>8081</port>
                    </connector>
                  </connectors>
                  <stopPort>18080</stopPort>
                </configuration>
              </plugin>
            </plugins>
          </build>
        </profile>
      </profiles>

    The stopPort is optional but I added in case you have two Maven jetty:run’s running at the same time on the same machine, the stopPort needs to be set to something other than the default on one of them.

  2. If you don’t want to use MAVEN_OPTS, you can do

    mvn jetty:run -Djetty.port=9090

Comments are closed.