Generate Web Application Project for Eclipse using Maven.

DS. picture DS. · Aug 16, 2010 · Viewed 8.9k times · Source

I'm new to this approach. I've used Maven, Tomcat and Eclipse for my web application. But I'm trying the approach where you create a Maven project using an archetype plugin.

My goal is to create a Web Application Project for Eclipse using Maven that can then be imported into Eclipse. I'm pretty sure there is a super-easy way to do this and I want to know what it is.

I'm using Tomcat 6, Eclipse Helios and Maven 2.

I was referring to this 3-part post:

http://united-coders.com/phillip-steffensen/maven-2-part-1-setting-up-a-simple-apache-maven-2-project

But when I imported the project into Eclipse, I couldn't see the Run As > Run on server option.

What is the best way to go about this? Any links to resources that'd help me understand the approach would be great!

Answer

Pascal Thivent picture Pascal Thivent · Aug 17, 2010

My goal is to create a Web Application Project for eclipse using maven that can then be imported into Eclipse. I'm pretty sure there is a super-easy way to do this and I want to know what it is.

Use the maven archetype plugin to generate your project. Here is how to tell it to use the maven-archetype-webapp when invoking it from the command line:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

But when I imported the project into eclipse, I couldn't see the Run As > Run on server option.

It actually all depends on what you use for Eclipse/Maven integration. There are basically two options (and they both provide WTP integration):

  • the maven-eclipse-plugin which is a Maven plugin that can generate Eclipse files (.project and .classpath and so on) allowing to import the project as Existing projects into Workspace.
  • the m2eclipse plugin which is an Eclipse Plugin providing Maven integration inside Eclipse and allowing to import a Maven project as Existing Maven projects.

The maven-eclipse-plugin approach

If you use the maven-eclipse-plugin, you have to configure it for WTP support and here is a typical configuration:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.8</version>
    <configuration>
      <projectNameTemplate>[artifactId]-[version]</projectNameTemplate>
      <wtpmanifest>true</wtpmanifest>
      <wtpapplicationxml>true</wtpapplicationxml>
      <wtpversion>2.0</wtpversion>
      <manifest>${basedir}/src/main/resources/META-INF/MANIFEST.MF</manifest>
    </configuration>
  </plugin>

With this configuration, running mvn eclipse:eclipse on your maven project will generate the WTP files so that the project can be recognized as a Dynamic project (i.e. runnable on a Server). Then import it via Import... > Existing projects into Workspace.

The m2eclipse approach

If you use the m2eclipse plugin (and that would be my recommendation), make sure to install the Maven Integration for WTP from the extras. From the install instructions:

Installing m2eclipse Extras

To install optional m2eclipse components, you will need to use the m2eclipse Extras update site. This update site contains the following m2eclipse components:

  • Maven SCM Integration
  • Maven SCM handler for Team/CVS
  • Maven SCM handler for Subclipse
  • Maven issue tracking configurator for Mylyn 3.x
  • Maven Integration for WTP
  • M2Eclipse Extensions Development Support

m2eclipse Extras Update Site: http://m2eclipse.sonatype.org/sites/m2e-extras

And then just import your project via Import... > Existing Maven projects and if it's a webapp, it should get recognized as a Dynamic project.

Indigo: m2eclipse approach for Indigo is different. See Maven/Tomcat Projects In Eclipse Indigo/3.7

Important: Note that both approaches are exclusive, use one or the other. But in both cases, there is no need to add a facet manually if you use them correctly.