How to create Servlet 3.0 web application in maven?
Asked Answered
M

4

9

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?

Monkish answered 28/7, 2013 at 16:58 Comment(2)
Modern archetypes here or hereKinnard
Related: Maven archetype for simple Servlet applicationBlubber
F
10

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.
Fearsome answered 21/9, 2015 at 21:24 Comment(2)
Wow, This worked like a charm. I was having trouble loading apache CXF servlet. Doing this resolved my issue. Thanks Roman !. Really appreciate it.Coronograph
Thanks alot for thisMessiah
J
3

The easiest way I've found is to create a Dynamic Web Project in eclipse and then convert it to Maven project:

  1. File -> New -> Dynamic Web Project
  2. Right click on project name: Configure -> Convert to Maven Project

The project will be configured with the latest version of the servlet

Then add the servlet and jsp dependencies to pom.xml:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
</dependency>
Jarrell answered 2/12, 2017 at 0:2 Comment(1)
this should be the accepted answer, after trying a lot of different ways to setup an eclipse web app to be deployed on aws, this is one of the most streamlinedAggregate
D
0

I usually create the 2.3 webapp then change it to 3.0 version. The steps are approximatly:

  1. In pom.xml: add maven-compiler-plugin configuration. source and target set to current java version (1.8), add servlet-api, jsp-api, jstl to dependencies.
  2. Delete web.xml.
  3. Open project properties (Alt+Enter on the project explorer)
  4. Click on Project Facets
  5. Uncheck checkbox for Dynamic Web Module, change version to 3.0
  6. Check checkbox back. Click further configuration, change WebContent to src/main/webapp.
  7. Generate Deployment Descriptor (in project explorer, right click Deployment Descriptor/Generate Deployment Descriptor Stub)
Declare answered 21/5, 2016 at 11:26 Comment(0)
Z
-1

In my case, servlet 3.0 is better compatible to jstl 2.0 3.0 by checking pom.xml <packaging> </packaging> use war (file format).

Zorina answered 26/5 at 1:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.