I get Maven. I get Eclipse & WTP. I want to use Maven for my dependency management, and I want to use Eclipse for it’s built in server suppport, for publishing, stopping and starting servers etc.
Several months back I took a look at M2Eclipse and it seemed it didn’t coexist well with WTP’s approach to web app directory structures. I’ve just taken another look and I got it working, but it took a few false starts and trial and error to find what works for me. In trying various things I’d either end up with a WTP style project which Maven wouldn’t build, or a Maven like directory structure which Eclipse wouldn’t build or deploy.
I’m pretty sure there’s many ways to do this, and I’m also sure Spring Tool Suite should be able to deploy a web project out of the box to your tc server without too much trouble, but it took a while to find what works.
One approach that does work but is probably not the best, is to add the tomcat-maven-plugin plugin to your pom.xml, define your Tomcat manager app userid and password in your maven settings.xml, and then deploy using the tomcat:deploy goal – this does work, but I’m pretty sure this is not the way you’re supposed to do things:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <configuration> <url>http://localhost:8080/manager/html</url> <server>tomcat</server> <path>/TestWebApp</path> </configuration> </plugin> </plugins> </build>
Add to settings.xml, in the servers section:
<server> <id>tomcat</id> <username>manager</username> <password>yourpassword</password> </server>
The way I think it’s supposed to work with the M2Eclipse plugin and STS, is you start with a web app project created via the M2Eclipse plugins archetype support, which sets up the project config exactly to work with WTP and deploy seamlessly to tc or other WTP supported servers:
File … New… Other… Maven… Maven Project… New
Set project location then Next. From the archetype list, select ‘maven-archetype-webapp’ ‘. Fill in the maven details, then Finish. That’s it.
When you select Run As… Run on Server, it will build automatically using your pom.xml and deploy to the server. Awesome. It’s pretty easy when you know how 🙂
One catch I noticed – the maven-archetype-webapp creates a web.xml which is for Servlet 2.3:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
This caused me a couple of hours trying to work out why my JSTL tags and EL were not working when deploying to Tomcat 7. Changing the web.xml to 2.5 fixed my jstl issues. Couple of hours of trial and error and plenty of Googling, ready to roll.
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">