20
votes

I'm trying to set up a basic hello world project using Eclipse Indigo and a Tomcat server. I created a dynamic project with a simple servlet. Tested the servlet and that worked fine. Then I enabled Maven support and added logback to my pom. I put a logging statement in the servlet's doGet method. When running the servlet, it complains it cannot find any bindings because the logback jars are not being copied into the Eclipse tomcat instance. I expected to find the jars published somewhere in here:

${workspace}\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\

How do I get Eclipse to work with WTP/Maven properly? I also tried installing the m2e-wtp connector with no difference.

4

4 Answers

51
votes

Check Deployment Assembly (context menu on project), there must be mapping Maven Dependencies -> WEB-INF/lib.

13
votes

Coming from an ASP.NET background, I find it shocking how much work it takes to get a webapp running with Eclipse WTP and Maven especially if you are learning on your own. Hopefully this quick start guide will help someone else get up to speed quickly.

There are two ways to get a hello world project working in Eclipse WTP with Maven. You can create a Dynamic web project and then add the Maven nature or you can do the opposite.

Pre-requisites for Eclipse with update sites

Startup configuration

Option 1: Create Dynamic Web Project then add Maven Nature

  • Create new Maven project, select archetype org.apache.maven.archetypes:maven-archetype-webapp

  • Change to Java EE perspective.

  • Create a new source folder, src\main\java. Notice how Eclipse is not smart enough to do this for you and also the ordering of the folders is incorrect. src\main\java folder is listed after src\main\resources. This can be manually fixed later in the project properties.

  • Create a new servlet. Notice how Eclipse defaults this file in the wrong folder src\main\resources because the order is wrong. Instead, manually select src\main\java. Change the URL mapping on the second page of the wizard to /* to make testing easier.

  • Now our servlet is ready but the dependencies on the servlet api are unbound. A) we can add the servlet api as a dependency to our project or B) we can bind to the Eclipse server config for Apache 7.0.

  • For option A, add this dependency to the pom:

.

<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-api</artifactId>
  <version>7.0.${set this}</version>
  <scope>provided</scope>
</dependency>
  • For option B:

    • Project properties -> Java Build Path -> Libraries -> Add Library -> Server Runtime -> Apache Tomcat 7.0
    • Right click and run on server:
  • A blank page should come up in the internal browser like http://localhost:8080/${artifact}

Test of dependency publishing:

  • Add joda-time to the pom.

  • Add this line in the servlet created earlier for the doGet method and import the necessary dependencies:

.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().println("The time is now: " + new DateTime().toString());
}

Reload the test page and the output should now be:

The time is now: 2012-03-03T14:14:29.890-05:00

Now if you want to play with Servlet 3.0 and annotations this is not enabled by default, for what reason I don't know. First force Maven to use Java 1.6 by adding this to your pom, otherwise each time you update your pom the configuration will revert to Java 1.5.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>

Open Project Properties -> Project Facets. Change the Version under "Dynamic Web Module" to 3.0, change java version 1.6

Create a new Servlet with class name AnnotatedServlet in src\main\java and notice how the @WebServlet annotation is auto created.

Option 2: Create Dynamic Web Project then add Maven Nature

  • Select Tomcat Runtime and Dynamic Module Version 3.0
  • Create source folder src\main\java
  • Set default output target\classes
  • Set context directory src\main\webapp
  • Check generate web.xml
  • Create servlet with mapping /* for quick testing
  • Add an output statement to the doGet method

response.getWriter().println("Another test");

  • Double click the "Deployment descriptor" and add this attribute to the root web-app element metadata-complete="false"

  • Right click project and select Run As -> Run On Server

  • Right click project -> Configure -> Convert To Maven Project
  • Select packaging as war
  • Edit pom and set compiler to use java 1.6 and add joda-time dependency:

.

<dependencies>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
1
votes

Right click on the web project in Project Explorer then choose Maven -> Update Project

0
votes

I faced a similar problem and although I had configured Deployment Assembly correctly it still didn't work. Then I found that under Window -> Preferences -> My Eclipse -> Java Enterprise Project -> Web Project, under the Deployment tab, the management of Dependent projects was turned off. I changed it to deploy jars of dependent projects to the lib folder and then everything worked like a charm. I could even turn off the Deployment Assembly option.