Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Web Application Project for Eclipse using Maven.

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!

like image 449
DS. Avatar asked Aug 16 '10 20:08

DS.


People also ask

How do I run a Maven webapp project in Eclipse?

Building and Running the Maven Project in Eclipse To run the maven project, select it and go to “Run As > Java Application”. In the next window, select the main class to execute. In this case, select the App class and click on the Ok button. You will see the “Hello World” output in the Console window.

How do I create a Maven project in Eclipse?

Create a new Maven project in Eclipse. From the File menu, choose New, and then choose Project. In the New Project window, choose Maven Project. In the New Maven Project window, choose Create a simple project, and leave other default selections.

Can we develop web application in Eclipse?

Eclipse helps you organize your web applications using a type of project called a Dynamic Web Project. When you create a Dynamic Web Project, you must select a Java EE web application server, which provides libraries needed by the project. Follow these steps to create the project. Select File > New > Other.

Is Maven used for web application?

You will create a Maven build definition file that's called a pom. xml file, which stands for Project Object Model, and use it to build your web application. You will then create a simple, automated test and configure Maven to automatically run the test.


1 Answers

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.

like image 83
Pascal Thivent Avatar answered Oct 10 '22 04:10

Pascal Thivent