reset password

Maven Web Application

Create an Maven web application in Eclipse using the maven-archetype-webapp archetype and do the following:

1. Change web.xml to the following:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0" id="springmvc">


    <display-name>Spring MVC Example</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

This change basically says that our project requires an application server that supports the Servlet 3.0 Specification.

2. Add the following to pom.xml (right after <finalName>):

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

This change says that the project requires JDK 1.7 (or above) to compile, and the compiled class files are compatible with JVM 1.7.

3. Due to some bug in Eclipse Kepler Release, Eclipse can no longer change project facets based on changes to web.xml and pom.xml, so we have to do it manually:

  • Close Eclipse.
  • Use a text editor to open the file .setting/org.eclipse.wst.common.project.facet.core.xml under the project folder (i.e. <workspace>/<project>), and change the version of java and jst.web to 1.7 and 3.0 respectively:

<installed facet="java" version="1.7" />
<installed facet="jst.web" version="3.0 />

  • Start Eclipse, Right click on the project and select Maven -> Update Project...

4. Remove the junit dependency (we'll use TestNG instead), the add the following dependencies to pom.xml:

  • javax.servlet:javax.servlet-api:3.0.1 (set the scope of the dependency to provided)
  • javax.servlet:jstl:1.2

This allows us to use classes like HttpServletRequest and HttpServletResponse in our Java code, and use JSTL in the JSPs.

5. Add a folder src/main/java. This is where the Java code would go.

6. Right click on the project and select Maven -> Update Project...

This page has been viewed 20215 times.