Starting Maven web project in Eclipse

We will create new web application project using Maven archetype and configure it for usage in Eclipse. Apache Maven must be installed and path to mvn command available in system variable PATH.

Generate Maven web application project

Run the following console command in parent directory of our new project:

mvn archetype:generate -DgroupId=com.example -DartifactId=formio-getstarted \ 
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

To import the project into Eclipse IDE, you can run command:

mvn eclipse:eclipse

in newly created formio-getstarted project directory. In Eclipse, choose File - Import... - General - Existing Projects into Workspace - select your formio-getstarted directory.

The following pom.xml is generated:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>formio-getstarted</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>formio-getstarted Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>formio-getstarted</finalName>
  </build>
</project>

Settings for Eclipse

In Eclipse, set these properties of the project:

  1. Properties on the project - Project Facets - Convert to faceted form... - check the Dynamic Web Module facet, choose version 3.0 and click "Further configuration available..."
  2. In the Content Directory field, type "/src/main/webapp". Check "Generate web.xml deployment descriptor" and click OK.
  3. Properties - Java Compiler - check Enable project specific settings - set Compiler compliance level to 1.7, check that appropriate Java version is selected also in Java Build Path - Libraries - JRE...
  4. Click Apply, then click OK.

Web application descriptor web.xml must conform to choosen Servlet 3.0 version - if this is still not true, replace its content with:

<web-app
  version="3.0"
  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">
    <display-name>Formio Getting Started</display-name>
</web-app>

Add desired version of servlet API to pom.xml:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

Run Maven - Disable Maven Nature in Eclipse, remove possible kind="var" dependencies from .classpath file, run mvn clean:eclipse in command line and then in Eclipse in the project context menu: Configure - Convert to Maven Project to overcome possible "Maven generated archetype versus Eclipse" integration problems. "Maven Dependencies" Classpath Container should now occur in Package Explorer and should contain dependencies of the project.

When you add new dependency to pom.xml (and save the file), new dependency should automatically occur in classpath - in Maven Dependencies Classpath Container.

We must configure appropriate deployment of Maven dependencies: In project Properties - Deployment Assembly - Click Add... - Java Build Path Entries - Maven Dependencies to extract dependencies to WEB-INF/lib directory when the application is deployed. In Properties - Java Build Path - Order and Export - check Maven Dependencies.

Configuring server

Now we can configure runtime environment (servlet container) for our web application: Under Properties - Targeted Runtimes - choose and configure Apache Tomcat 7.0. Basic configuration will be sufficient. You will need to download and extract Apache Tomcat 7 binary distribution to some directory if you have not any server available yet. Select runtime and click OK, new Apache Tomcat classpath container should occur in the project.