Tuesday, April 20, 2010

Use Maven Jetty Plugin in Eclipse

I wanted to run Jetty in Eclipse so I can write code, debug, test, etc. Ran into some issues. Solution here.


Add Jetty plugin to your pom file:

<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
</plugins>
<builds>

I then tried to run the jetty goal from the command line to keep it simple:

mvn jetty:run

No user specific settings.xml file

Then, something was griping because I never added a settings.xml file to my local repository. Copy the settings.xml file from the directory where you installed maven and drop it into your local repository (see my post on Java development environment if you need more info).

Missing Dependencies

Next...complaints about missing jars not in Maven central repository.

Download the jars I found searching in Google (what would we do without Google as nothing Java related seems to be well documented completely from end to end):

el-impl-2.2.jar

http://download.java.net/maven/2/org/glassfish/web/el-impl/2.2/

el-api-2.2.jar

http://download.java.net/maven/2/javax/el/el-api/2.2/

jsp-impl-2.2.jar

http://download.java.net/maven/2/org/glassfish/web/jsp-impl/2.2/

jsp-api-2.2.jar

http://download.java.net/maven/2/javax/servlet/jsp/jsp-api/2.2/

Install into maven repository with the following commands (make sure you're in the directory where you downloaded the jars or provide full path to jars):

mvn install:install-file -DgroupId=org.glassfish.web -DartifactId=el-impl -Dversion=2.2 -Dpackaging=jar -Dfile=el-impl-2.2.jar

mvn install:install-file -DgroupId=javax.el -DartifactId=el-api -Dversion=2.2 -Dpackaging=jar -Dfile=el-api-2.2.jar

mvn install:install-file -DgroupId=org.glassfish.web -DartifactId=jsp-impl -Dversion=2.2 -Dpackaging=jar -Dfile=jsp-impl-2.2.jar

mvn install:install-file -DgroupId=javax.servlet.jsp -DartifactId=jsp-api -Dversion=2.2 -Dpackaging=jar -Dfile=jsp-api-2.2.jar

PluginsGroup

Next I had to add this to my settings.xml file in my local respository settings.xml file:

<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>

No webapp directory or start up file

I noticed that Jetty was referencing a non-existant path for the web app. Default is under your project /src folder in /webapp directory. I also added the default index page under my project src directory:

/src/webapp/index.html

Bug: Jetty context fails with spaces in path

Ok the next issue was that apparently if you have spaces in the path to your local repository Jetty will fail for that reason also.

Details are here: http://jira.codehaus.org/browse/JETTY-1166

So...I had to move my whole local repository to an alternate directory with no spaces. I uncommented the local repository setting in the settings.xml file and changed the path to the new location.

<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository-->
<localRepository>/path/to/local/repo</localRepository>

Finally...Jetty runs. If you got this far test it out by going to this url on your local machine:

http://localhost:8080

Running from within Eclipse

Okay that's nice but I wanted to run Jetty from within Eclipse. I right clicked on project pom.xml file, chose "Run As" then "Run Configurations". Double clicked on "Maven Build" node to add a new Maven run configuration. In the dialog box I typed a name - called it "run jetty". I entered the path to the directory containing my pom.xml file as the base directory. For goals I entered "jetty:run". I checked Debug Output, update snapshots and resolve workspace dependencies - clicked Apply, then run.

Yippee. Jetty is running in Eclipse and you can see the output in the Eclipse console.