www.rodneybeede.com "I would love to change the world, but they won't give me the source code" - unknown
 Navigation

Maven project as Eclipse Dynamic Web Project - Last Modified 2013-09-11 19:35 UTC - Created 2013-09-11 19:33 UTC

I used:

Creating the project

  1. mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
  2. Project is created

Create the Eclipse project

  1. Change into the project folder
  2. Modify the pom.xml with:
    1. under <build> <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                  <downloadSources>true</downloadSources>
                  <downloadJavadocs>true</downloadJavadocs>
                  <wtpversion>2.0</wtpversion>
                </configuration>
              </plugin>
        
    2. Or do mvn eclipse:eclipse -Dwtpversion=2.0
  3. mvn eclipse:eclipse
  4. Import the project in Eclipse with "Existing Projects into Workspace"

Get it to actually run

Next we must modify some variables in Eclipse in order to get it to actually build and run the project.

  1. Window menu, Preferences
  2. Server, Runtime Environments
  3. Add...
  4. Pick your web container server
  5. Next to follow setup then Finish
  6. Java, Build Path, Classpath Variables
  7. New...
  8. Name (case sensitive) of M2_REPO
  9. Path of C:/Users/youruser/.m2/repository
  10. OK to save
  11. Say yes to the full rebuild
  12. Right click on your project in the Project Explorer
  13. Run As
  14. Run on Server
  15. Pick your server and run your app

Maven servlet dependencies

A helpful list of how to resolve servlet-api names for newer versions (it is very inconsistent on all the Maven repos) can be found at http://maven.apache.org/guides/mini/guide-coping-with-sun-jars.html, but beware that newer releases of the servlet apis don't seem to follow these recommendations anymore.

Sample pom.xml

<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>TLD.EXAMPLE.GROUP</groupId>
  <artifactId>YOUR-ARTIFICAT-ID</artifactId>
  <packaging>war</packaging>
  <version>-SNAPSHOT</version>
  
  <name>YOUR-PROJECT-NAME Maven Webapp</name>
  <url>http://www.rodneybeede.com</url>
  <inceptionYear>2013</inceptionYear>
	<licenses>
		<license>
			<name></name>
			<url></url>
			<distribution>repo</distribution>
			<comments></comments>
		</license>
	</licenses>
  <organization>
		<name>SOME ORG</name>
		<url>http://www.example.com/</url>
	</organization>
  <developers>
		<developer>
			<id>rbeede</id>
			<name>Rodney Beede</name>
			<email>[email protected]</email>
			<url>http://www.rodneybeede.com/</url>
			<organization>TODO</organization>
			<roles>
				<role>architect</role>
				<role>developer</role>
				<role>tester</role>
			</roles>
		</developer>
	</developers>
	<contributors>
		<contributor>
			<name></name>
			<email>[email protected]</email>
			<organization></organization>
			<roles>
				<role>tester</role>
			</roles>
		</contributor>
	</contributors>
  
  	<dependencies>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>apache-log4j-extras</artifactId>
			<version>1.1</version>
		</dependency>
		
		<!-- Maven Central and other repos don't have consisent locations for versions of the J2EE APIs
			This guide was helpful for older versions, but it doesn't seem to be followed anymore in repos:
			http://maven.apache.org/guides/mini/guide-coping-with-sun-jars.html
			You can also search http://jcp.org/en/jsr/all to get the middle point (x.'#'.z) latest JSR spec version
		-->
		
		<!-- http://www.java.net/forum/topic/glassfish/glassfish/javaxservlet-api-version -->
		<!-- http://java.net/projects/servlet-spec -->
		<!-- Found in Maven central repository -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		
		<!-- http://jsp.java.net/downLoad.html -->
		<!-- Found in Maven central repository -->
		<dependency>
			<groupId>javax.servlet.jsp </groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.3.1</version>
			<scope>provided</scope>
		</dependency>
		
		<!-- Find the latest version with groupId and artifactId per http://jstl.java.net/download.html -->
		<!-- Found in Maven central repository -->
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.servlet.jsp.jstl</artifactId>
        <version>1.2.2</version>
    </dependency>
	</dependencies>
  
  <build>
    <finalName>YOUR_WEBAPP_CONTEXT_PATH_NAME</finalName>
    
    <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<configuration>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>true</downloadJavadocs>
					<wtpversion>2.0</wtpversion>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
  </build>
</project>

Sample web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Java Servlet Specification version 3.1 final document has an old example 
	of only version 2.5. This website (http://antoniogoncalves.org/2013/06/04/java-ee-7-deployment-descriptors/) 
	provided the updated example of the web-app tag -->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">

	<display-name>YOUR_WEBAPP_NAME</display-name>

	<context-param>
		<description>Location where the web application configuration can be found.  We keep the configuration
		options separate from the web.xml because it makes redeployment easier to avoid having to update web.xml for
		each environment.</description>
		<param-name>Pathname to configuration file</param-name>
		<param-value>/etc/WebAppConfig.xml</param-value>
	</context-param>

	<listener>
		<description>Responsible for initializing the web application at startup</description>
		<listener-class>TLD.DOMAIN.WEBAPP.servlets.InitializationListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>SomeServlet</servlet-name>
		<servlet-class>TLD.DOMAIN.WEBAPP.servlets.SomeServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>SomeServlet</servlet-name>
		<url-pattern>/servlets/SomeServlet</url-pattern>
	</servlet-mapping>

	<security-constraint>
		<web-resource-collection>
			<web-resource-name>HTTP Method Security</web-resource-name>
			<description>Restricts HTTP methods to only allowed methods for best security practice</description>
			<url-pattern>/</url-pattern>
			<http-method-omission>GET</http-method-omission>
			<http-method-omission>POST</http-method-omission>
		</web-resource-collection>
	</security-constraint>
</web-app>