Maven as Eclipse Dynamic Web Module

This quick tutorial will show you how to create a Maven project with a Dynamic Web Module facet. It uses m2e to generate the Maven project.

1. Run Eclipse and switch to the Java EE perspective.

2. Create a new Maven project. Pick the spring-mvc-jpa-archetype to get a fully configured Spring 3 MVC application.


3. If you don’t have a configured Eclipse Server, add one now. Here we will register a new Tomcat 7 server. Open the Servers view, right click -> New -> Server.

4. Hit next and enter the Tomcat installation directory.

When you hit next, notice you will not see any available resources that you can move to the server.

We still have to configure our new Maven project to become a Dynamic Web Module. This is our next step. Hit Finish to complete the new Server setup.

5. Now right click on the project and click Properties. Go to Project Facets.

Click on “Convert to faceted form” and pick “Dynamic Web Module” version 2.5. Picking version 2.5 makes it compatible with older web containers. Click “Further configuration available”, enter “srcmainwebapp” for the content directory, and hit OK.

6. Now to make the Maven dependencies available to the Dynamic Web Module, go to Project Properties again and click on Deployment Assembly. Click Add… and pick “Java Build Path Entries”. Hit next and pick Maven Dependencies.


7. Our project is now fully configured. To test, go to the Servers view, right click on the Tomcat Server and hit “Add and Remove…”. Pick mavenweb from the available resources and add it to the Server.

Start the server and go to http://localhost:8080/mavenweb. You should see

Now you have a Maven project fully configured as a Dynamic Web Module.

If you want to create the Maven project using mvn eclipse:eclipse, you have to configure the wtpversion.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-eclipse-plugin</artifactId>
	<version>2.8</version>
	<configuration>
		<wtpversion>2.0</wtpversion>
	</configuration>
</plugin>

M2Eclipse (m2e) Support for Maven Eclipse Plugin Projects

If you have a Maven based Eclipse project that you generated using mvn eclipse:eclipse, you’ll notice you won’t be able to use M2Eclipse (m2e) with it. If you look at the Eclipse .project file, you’ll see this comment.

NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.

Here’s how you can convert your Maven Eclipse Plugin project to m2e. First add the M2Eclipse builder under the buildSpec tag, and the M2Eclipse nature under the natures tag, both inside the .project file.

...
<buildCommand>
  <name>org.eclipse.m2e.core.maven2Builder</name>
</buildCommand>
...
<nature>org.eclipse.m2e.core.maven2Nature</nature>

Next step is to replace all the M2_REPO libraries in the .classpath file with the M2Eclipse classpath container.

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>

Refresh your Eclipse project. Right click on the project in Project Explorer View and you should now see the Maven option.


Java 7 on Eclipse Indigo

Update: Eclipse Indigo 3.7.1 now has built-in support for Java 7. The post below only applies to Eclipse Indigo versions below 3.7.1.

Here are the steps needed to start playing with the new Java 7 language changes (aka Project Coin) in Eclipse Indigo.

1. Download and install Java SE 7 JDK. The JDK installer also comes with the JRE installer.

2. A few days ago, you needed the Eclipse Java Development Tools Patch for Java 7 Support (BETA). from the Eclipse update site http://build.eclipse.org/eclipse/java7patch/. This is no longer available and developers have to download a maintenance build. Download the latest version of Eclipse 3.7 from the 3.7.x Stream Builds section. You now should be able to use the Java 7 compiler. Also verify the Installed JREs in Eclipse.

3. Create a new project using the Java 7 compiler and library.

4. Read Project Coin to know more about the language changes introduced in Java 7.

5. Create a new class to try out the language changes in Eclipse. Try the code below.

package com.teamextension.java7;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ProjectCoin {
	public static void main(String[] args) throws Exception1, Exception2 {
		stringInSwitch();
		numericLiterals();
		diamond();
		tryWithResources();
		multiCatch();
		preciseRethrow();
		simplifiedVarArgs();
	}

	private static void stringInSwitch() {
		switch ("apple") {
		case "apple":
			System.out.println("cider");
		}
	}

	private static void numericLiterals() {
		System.out.println(1_000_000);

		System.out.println(0b1000);
	}

	private static void diamond() {
		List<String> list = new ArrayList<>();
	}

	private static void tryWithResources() {
		try (BufferedReader br = new BufferedReader(new FileReader("README.txt"))) {
			System.out.println(br.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
		// no more finally
	}

	private static class Exception1 extends Exception {
	}

	private static class Exception2 extends Exception {
	}

	private static void multiCatch() {
		try {
			if (true) {
				throw new Exception1();
			} else {
				throw new Exception2();
			}
		} catch (Exception1 | Exception2 e) {
			e.printStackTrace();
		}
	}

	private static void preciseRethrow() throws Exception1, Exception2 {
		try {
			if (true) {
				throw new Exception1();
			} else {
				throw new Exception2();
			}
		} catch (Exception e) {
			throw e; // notice Exception1 and Exception2 in the throws clause
		}
	}

	private static void simplifiedVarArgs() {
		// See http://download.oracle.com/javase/tutorial/java/generics/non-reifiable-varargs-type.html
		List<String> list = null;
		asList(list);
	}

	@SafeVarargs
	private static <T> List<T> asList(T... elements) {
		// Type safety: Potential heap pollution via varargs parameter elements
		return null;
	}
}

The code is self explanatory, and includes some comments about the not so obvious parts of the code. What requires further reading is the Simplified varargs method invocation. I suggest you read Using Non-Reifiable Parameters with Varargs Methods for more information.