Search

Dark theme | Light theme

September 12, 2008

Shutdown Jetty started by Maven in NetBeans

We can start Jetty with the jetty:run command in NetBeans, but because of an issue for Windows users (see issue 138116) we cannot shutdown Jetty from within NetBeans. We can of course start the Windows TaskManager and kill the process by hand, but wouldn't it be nice of we could just stop Jetty from within NetBeans?

Luckily this is not so difficult to achieve. First we must change our pom.xml and add some elements to the Jetty plugin definition. Let's look at a sample from our project's pom.xml:

<plugin>
 <groupId>org.mortbay.jetty</groupId>
 <artifactId>maven-jetty-plugin</artifactId>
 <version>6.1.7</version>
 <configuration>
   <connectors>
  <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
    <port>8888</port>
    <maxIdleTime>30000</maxIdleTime>
  </connector>
   </connectors>
   <contextPath>/</contextPath>
 </configuration>
</plugin>

Now we must add stopKey and stopPort elements to the configuration element. The stopKey is a text string supplied with the stop command. And the stopPort is the port number Jetty will listen to for stop commands. So now we get the following definition (see line 12-13):

<plugin>
 <groupId>org.mortbay.jetty</groupId>
 <artifactId>maven-jetty-plugin</artifactId>
 <version>6.1.7</version>
 <configuration>
   <connectors>
  <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
    <port>8888</port>
    <maxIdleTime>30000</maxIdleTime>
  </connector>
   </connectors>
   <contextPath>/</contextPath>
   <stopKey>stop</stopKey>
   <stopPort>8889</stopPort>
 </configuration>
</plugin>

Next we must invoke the jetty:stop command to stop a running Jetty instance. We right-click on our project and select Custom | Goals.... We get a dialog window where we can fill in values. We fill the Goals field and Remember as field with the value jetty:stop. We click the OK button to close the dialog.

And now we can execute the jetty:stop command right from within NetBeans. We right-click on our project, select Custom | jetty:stop and NetBeans will stop a running Jetty instance.