Maven 2 project as Eclipse Dynamic Web Project

Last Modified: Fri, 30 Nov 2012 22:02:38 +0000 ; Created: Thu, 29 Jan 2009 21:48:00 +0000

I used:
  • Maven 2.2.1
  • Java 1.6.0_22
  • Eclipse IDE for Java EE Developers, Helios SR1, 3.6.1 (Build id: 20100917-0705)

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"

Sample pom.xml

Old web project I did in school

A helpful list of how to resolve servlet-api 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.

Another sample POM:

<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.rodneybeede</groupId>
	<artifactId>TODO-ARTIFACT</artifactId>
	<packaging>war</packaging>
	<version>-SNAPSHOT</version>
	
	<name>TODO</name>
	<url>http://TODO/</url>
	<inceptionYear>2012</inceptionYear>
	<licenses>
		<license>
			<name></name>
			<url></url>
			<distribution>repo</distribution>
			<comments></comments>
		</license>
	</licenses>
	<organization>
		<name>TODO</name>
		<url>http://www.TODO.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>
			<url></url>
			<organization>TODO</organization>
			<organizationUrl>http://www.TODO.com/</organizationUrl>
			<roles>
				<role>TODO</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.0.1</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.2.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.javax.servlet.jsp</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2.1</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>TODO-DEFAULT-WEBAPP-CONTEXT-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.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

Get it to actually run

Next we must modify some variables in Eclipse in order to get it to actually 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. Run/Debug, String Substitution
  13. New...
  14. Name (case sensitive) of M2_REPO
  15. Value of %USERPROFILE%\.m2\repository
  16. OK to save
  17. Right click on your project in the Project Explorer
  18. Run As
  19. Run on Server
  20. Pick your server and run your app