Tuesday, March 27, 2007

How to set up an Eclipse Web Project with Maven 2

I wanted to use maven's very useful capability to download dependencies when setting up an eclipse project with wicket. It was the first time for me using wicket so I wanted to get up and running fast.

First I downloaded and installed maven 2 from http://maven.apache.org/.

A useful site was http://mvnrepository.com/. I searched for wicket.

Then I created a maven project (on Windows). In a command window:

C:\EclipseProj\mvn-app1>mvn archetype:create -DarchetypeGroupId=org.apache.maven
.archetypes -DgroupId=com.mycompany -DartifactId=mvn-app1

Then I edited C:\EclipseProj\mvn-app1\pom.xml to add the wicket dependency that I found at mvnrepository.com:


Then, I created the eclipse artifacts for my project. In my case it was a web (WTP) project. Note you need to pay attention to the version of WTP you are using:
C:\EclipseProj\mvn-app1>mvn -Dwtpversion=1.5 eclipse:eclipse

Unfortunately this was not all I had to do. For some reason the project was not configured as a Dynamic Web Module, as I required. I needed to edit:


and change the file contents to (or copy it from a new Dynamic Web Module project):

...XML header...
<runtime name="JBoss v4.0" />
<fixed facet="jst.web" />
<fixed facet="jst.java" />
<installed facet="jst.web" version="2.3" />
<installed facet="jst.java" version="1.4" />

Note that in my case, my target runtime is JBoss.

I also needed to create a WebContent directory, which is easiest to do by copying it from a new Dynamic Web Module project.

Now you can open your project in eclipse. Use File-->Import-->Existing Projects Into Workspace.

I found I got an error that the "Java compiler level does not match the version of the installed java facet". This means that the maven target set the java version to 1.4, but I had 1.5 installed. It was easy to fix by right clicking on the error in the Problems window and selecting Quick Fix. This is not the same as changing the line in the file org.eclipse.wst.common.project.facet.core.xml to 1.5.

No comments: