How to create Servlet 3.0 web application in maven?

Ramsharan picture Ramsharan · Jul 28, 2013 · Viewed 10.4k times · Source

When I use eclipse to create webapp with maven using "maven-archetype-webapp", it creates only Servlet 2.3. How can I create Servlet 3.0?

Answer

JJ Roman picture JJ Roman · Sep 21, 2015

There is still no good way of doing this.

Eclipse is parsing web.xml to indentify project's facet and therefore servlet version.

To achieve servlet 3.0 web app in eclipse follow these steps:

Using Eclipse only:

  1. Create MVN project of maven-archetype-webapp New -> Project -> Mvn Project
  2. Replace web.xml file with new 3.0 version:

    <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
          <display-name>Archetype Created Web Application</display-name>
        </web-app>
    
  3. Close project and delete it from the workspace (don't delete files on the disk)

  4. Delete .project and .classpath files and .settings directory from the project folder
  5. Reimport project using import -> Existing Maven Project

Using MVN commandline + Eclipse

  1. Create MVN project of maven-archetype-webapp

      mvn archetype:generate 
          -DarchetypeGroupId=org.apache.maven.arechetypes 
          -DarchetypeArtifactId=maven-archetype-webapp 
          -DarchetypeVersion=1.0 
          -DgroupId=<my.groupid> 
          -DartifactId=<my-artifactId>
    
  2. Replace contents of web.xml as in eclipse method point 2.

  3. Same as point 5 from eclipse method.