Spring STS, Eclipse WTP, and building web apps with Maven

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.

2.5 web.xml:

<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">

2 Replies to “Spring STS, Eclipse WTP, and building web apps with Maven”

  1. Hi, we’re setting up a new project using STS, Maven (maybe Gradle), etc. Tomcat web app using Spring 3.1. I’m in the habit of separating my projects in eclipse to help with independence among development groups. So we create separate pure java projects, and the web app similar to how you describe above – and when I want to run/debug the app, I want it to be seamless like you say – knows to execute the maven build(s) etc. With a separate referenced project though – Eclipse/STS does not automatically listen to changes to referenced projects it appears. When I change a src file, it should automatically reflect in the web project. Have you done this? Guidelines on project type for referenced project?

    So far I’ve only been able to figure out using manual methods – like include commands to copy the jar file after building the referenced project, then use clean or refresh on the web app to get the changes. Haven’t used Maven that much on previous projects in eclipse – so I’m sure I just haven’t seen it done right yet. I’m sure there’s a way – as it is I feel like I’m ‘forcing’ the solution.

    Apologies if you feel this is not appropriate for this blog entry.

    – Dan C.

    1. Hi Dan – the manual/forced way you describe works, but I believe the maven way would be to setup a pom.xml file at the parent directory level that contains each of your sub/related/dependent projects, and declare each of the sub projects as maven modules. I haven’t done this before either, but I have taken the same manual approach you described before I was wondering what the best way to do this would be. I didn’t get as far as trying it out, but I think a maven multi module structure is the way to do it.

      Kevin

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.