views:

165

answers:

2

Hi,

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.

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!

+1  A: 

Download and install the eclipse maven plugin from here. Create your project using the new project wizard in eclipse. Select Maven project and create the project using the archetype you discussed. Set appropriate source folders and add libraries used as part of project properties. This should set you up for your project.

Tushar Tarkas
Ah... thanks!At least I was right about the super-easy part! :)Thank you for the quick response. Those who are trying this out... you might also need to install a facet to the project in the project properties after it is generated to run it on server.
DS
+3  A: 

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.

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.

Pascal Thivent
I'm marking this one as the accepted answer because of the details and also explaining the options available for solving the problem at hand. Thank you!
DS
@DS You're welcome, glad you found it useful.
Pascal Thivent