Eclipse WTP not publishing Maven dependencies

23,219

Solution 1

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

Solution 2

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>

Solution 3

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

Share:
23,219
dcompiled
Author by

dcompiled

Updated on July 09, 2022

Comments

  • dcompiled
    dcompiled almost 2 years

    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.

  • dcompiled
    dcompiled about 12 years
    Thanks for the tip, this was correctly configured but not working. Eventually I found out that creating a new project from scratch worked. In this case I had created the project and then added the Maven WTP connector and it did not configure the project correctly.
  • bh5k
    bh5k over 11 years
    I had a little bit of a different problem, but this fixed it. I was using Maven to build my project which had SNAPSHOTS in it. I originally had Workspace Resolution enabled and turned it off. Eclipse was configured to still use the local project and not the snapshots from our nexus repo. I kept getting CNFEs and found this which pointed me to the problem of it still trying to resolve it and not publish it.
  • Alex
    Alex about 11 years
    this helped me a lot and in another case :) (similar)
  • Amr Eladawy
    Amr Eladawy over 8 years
    As more clear steps, 1. Right click on the project 2. click properties 3. choose Deployment Assembly. 4. (if not there) click add 5. choose (Java build path entries) 5. Choose Maven Dependency
  • Periata Breatta
    Periata Breatta over 7 years
    Things seem to be somewhat better these days with more recent versions of eclipse (my process: create new maven project, select the web app archetype, add the maven dependencies to the deployment assembly as described in the accepted answer here, which seems to generally work), but the fact that this question is still relevant shows that it's still not perfect. But neither the web application system as it exists nor maven were actually designed with the use of IDEs in mind, which is probably why we have issues like this. Whereas everything in .NET was designed to integrate with VS nicely.