Generate Web Application Project for Eclipse using Maven. - eclipse

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!

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.

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.

Very simple,
you only have to create a new Maven project with packaging type ‘war’ and directories creation done automatically

Related

How to do the `mvn eclipse:eclipse` through the M2Eclipse plugin?

It came to my attention that:
The maven-eclipse-plugin (used in mvn eclipse:eclipse) is discontinued and shouldn't be used anymore. M2Eclipse replaces it for most, if not all, usage.
I work on a new project now, and the M2Eclipse doesn't update automatically the changes in my eclipse project.
My question is:
How to do the mvn eclipse:eclipse through the M2Eclipse plugin?
I believe it's equivalent Import an existing Maven Project. Elipse will do rest of the things. You might need to change your project to maven type.
Configure>Convert to Maven Project

How to deploy a maven project in tomcat within Eclipse

I have been trying to deploy a project that I created within eclipse but when I right click on the tomcat server and click "Add and Remove...", I got the message "There are no resources that can be added or removed from the server".
From what I understand it has something to do with the facets? I am not really sure to understand what is going wrong here.
Here is what I did for know:
I created a Maven project.
executed mvn eclipse:eclipse through the command line on the folder of the project
installed m2e
tried to add the project to the tomcat server
You probably want to use either mvn eclipse:eclipse OR m2e, but not both. m2e has gotten MUCH better in the last couple years, so that's what I would suggest.
Make sure you are using Eclipse for Java EE, otherwise you will need to install other plugins like WTP
Install m2e.
Install maven integration for wtp (from the eclipse marketplace)
make sure the artifact type for your project is war
Import Existing Maven Project in eclipse and browse to your pom to import the project.
It should give the correct facets to your project so it looks like a web project.

Create an eclipse plugin with dependency on a maven project

I have a maven project that contains a certain api I need to use in an eclipse plugin. This eclipse plugin is not currently a maven project but a normal eclipse plugin project with a manifest. I converted this plugin project to a maven project (using m2e menu command to change project to maven) I added the dependency to the maven project from this project. My maven build for this project is running fine from command line. Now when I launch the eclipse application I am getting a ClassNotFoundException for the api I am referring to from the plugin project. Please help.
This can be done by adding tyco configuration to the maven project. That enables you to have a maven project with a Manifest thus enabling other plugins to depend on it. More info: http://www.eclipse.org/tycho/

Eclipse:Run on server option not visible for maven project

I'm running through the jboss as7 getting started guide here http://hudson.jboss.org/jenkins/job/JBoss-AS7-Docs/lastSuccessfulBuild/artifact/guides/developer-getting-started-guide/target/docbook/publish/en-US/html/helloworld.html . The tutorial has us setting up an example helloworld quickstart maven project.
I'm able to deploy this project from the command line successfully
mvn package jboss-as:deploy
but when I attempt to deploy the helloworld example from Eclipse - the 'run on server' option is missing from the run menu. I have Eclipse 3.7 and maven wtp installed.
Not sure how to fix, any advice appreciated.
The run on server related options only appears if your project has the Dynamic Web Moudle project facets.
You can try to configure it using the Project Facets options in your project properties
To help any other developer with this problem.
I was dealing with this issue recently. Maven projects are structured differently than Dynamic Web Projects. So when you manually add the Dynamic Web Module using Project Facets, eclipse may not register it properly as a Maven project.
To solve this, you have to install the m2e plugin and M2E Eclipse WTP plugin (this tells Eclipse how to run your maven projects).
Go to Help -> Eclipse Marketplace to search for the plugins.
After installing, you will need to restart Eclipse then you will be able to use "run on server" for your Maven projects.
Note: You may need to remove/delete the previous project then import/create it again after the restart.
You have to add maven eclipse pluginin your pom.xml file..
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
</plugin>
According to Red Hat's site, The M2E Eclipse WTP plugin (m2eclipse-wtp) has been deprecated in favor of the newer m2e-wtp plugin. If you don't want to go through the marketplace, the URL for the new plugin is http://download.eclipse.org/technology/m2e/releases.
After copying the project and refreshing it, closing and reopening it, removing and re-adding it (right-click on server) and updating Maven... the option reappeared.
I think it occurs when you up the version of the Java compiler in your .pom file, when you up the compiler in project properties or it has to do with using the newer Jersey (2) version or when you both include local libraries and maven dependencies, it may also be a consequence of a combination of these. It's pretty unclear.
Note though, that you can still add/remove projects by right-clicking on the server in the servers tab.

How can I add a classpath entry as a publish/export dependency in an Eclipse dynamic web project?

I successfully created a project using Wicket quickstart and turned it into an Eclipse dynamic web project by running
mvn eclipse:clean eclipse:eclipse -Dwtpversion=2.0
I imported the project to Eclipse without any issues, but got this warning for each JAR:
Classpath entry M2_REPO/**.jar will not be exported or published.
Runtime ClassNotFoundExceptions may result.
I can fix this by using right click → QuickFix on each warning and selecting "Mark the associated raw classpath entry as a publish/export dependency," but this takes a lot of time and would not be possible if there were a lot of dependencies.
There must be a way to have Maven do this for me; what is it?
EDIT: I've found out that using m2eclipse core + Maven Integration for WTP (from m2eclipse extras) resolves my issues.
I'm still interested in how to achieve this without m2eclipse, though, just out of curiosity :p
The two Maven plugins needed to work with web projects in Eclipse are available from the Eclipse Marketplace.
Maven Integration for Eclipse (included in the Java version of Eclipse)
Maven Integration for Eclipse WTP