Loading...

Wednesday, February 11, 2009

Use Jetty to run Maven web applications in NetBeans

Jetty is a great web server. It is fast to start up and that makes it very useful during development. Jetty has a Maven plugin we can use in our Maven web application project. This way we can use Jetty to test our JSP files. We can change the JSP file and hit the Refresh button of our browser to see the result immediately. We can even compile a Java class file and Jetty will restart so we can test the result in our browser.

Let's see how we can do this in NetBeans. We go to File | New Project, select Maven Project and click the Next button.

In the next dialog window we select Maven Webapp Archetype and click the Next button.

Now we can fill in the values for our project and press the Finish button.

NetBeans creates a new project for us. To add Jetty Maven plugin we must open the pom.xml file for our project. In the build section we must add the following code:

<plugins>
 <plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <version>6.1.14</version>
  <configuration>
   <scanIntervalSeconds>5</scanIntervalSeconds>
  </configuration>
 </plugin>
</plugins>

At line 6 we define how often Jetty will scan the source directories to look for changes. Because we add this definition we can change JSP files and see the result in our web browser without restarting the server.

Okay, we have defined Jetty for our project, but how can we run Jetty in NetBeans? We go to File | Project Properties and select the Actions category. We must look for the action Run project and select it. In the Execute goals field we add jetty:run. This will start Jetty if we run our project. We can close the dialog window.

Now we go to Run | Run Project to start Jetty. In the Output window of NetBeans we see the following text:

Starting jetty 6.1.14 ...
2009-02-11 10:20:20.610::INFO:  jetty-6.1.14
2009-02-11 10:20:20.982::INFO:  No Transaction manager found - if your webapp requires one, please configure one.
2009-02-11 10:20:21.335::INFO:  Started SelectChannelConnector@0.0.0.0:8080
Started Jetty Server
Starting scanner at interval of 5 seconds.

At line 3 we see Started SelectChannelConnector@0.0.0.0:8080 this means Jetty is available at port 8080. We open a web browser and open http://localhost:8080/webappjetty/ and we see the following:

We go back to NetBeans and open the file index.jsp. We add the following to the file:

<h3>It works!</h3>

We switch back to our web browser and reload the page and we get:

13 comments:

Kelly Davis said...

Thanks for the post. Any thoughts on debugging?

akochnev said...

If you want to debug your Jetty that you ran from maven, you need to just pass maven the standard java debugging flags (e.g. -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=y ) and then use the IDE debugger to connect to the designated port (e.g. port 4000 in the example above). There is no way to configure the same from the maven plugin itself.

mrhaki said...

@Kelly Davis: akochnev is right. You need to run mvnDebug jetty:run outside of NetBeans and attach the debugger from NetBeans. mvnDebug sets all the correct debugging flags and can be found in the bin folder of your Maven2 directory.

Joe said...

Hey I did all steps but when I execute Run | Run Project Netbeans shows to me a popup asking for choose a deployment server, if I choose one of the list netbeans start that server after finish build project, if I dont choose any server the process of Run dont start.
Any help?

Joe said...

Hey I did all steps but when I execute Run | Run Project Netbeans shows to me a popup asking for choose a deployment server, if I choose one of the list netbeans start that server after finish build project, if I dont choose any server the process of Run dont start.
Any help?

4Ndr3 4nD3r1 said...

The same here =/

mrhaki said...

@Joe and 4Ndr3 4nD3r1: The described solution is for NetBeans 6.5. In later NetBeans versions the Maven plugin has changed and then we have to define a server in NetBeans and select it. Jetty is not an option for a defined server in NetBeans.

customrobotics said...

Hi im having the same problem with Netbeans 6.8. When I run the project Netbeans wants me to choose a deployment server, but I cant choose Jetty. How do I add Jetty to this list or is there another way?

Thanks, great guide.

Anonymous said...

regarding the problem you have to debug with jetty under netBeans:

go to project properties, in "Actions", create a new one you will call i.e. "MyServer Debug"

type in the field "execute goal" : jetty:run

in "Set properties": jpda.listen=maven

that's all :)

then to start the app in debug mode, right clic on the project, select "Custom" and then the new Action you have defined, so "MyServer Debug".

cheers

skoczko said...

Actually, changing 'Run' action to jetty:run is not good enough - running every consecutive time won't work (jetty is already started and cannot ind to address), you need to specify the run action as:

jetty:stop jetty:run

and put the following in pom.xml:



org.mortbay.jetty
maven-jetty-plugin

9966
some-key

chameleon said...

@customrobotics: thank you for the nice trick to debug..
thanks to blog owner. nice blog. nice share

Anonymous said...

Thanks anonymous, your simple settings showing how to debug worked just fine for me, in Netbeans.

Anonymous said...

Hi,

Thanks for the post!

@Joe: In NB 7.0 I have an Option in the Popup "Do not use any AppServer (for jetty, etc.)".

Post a Comment