Running Maven project within Eclipse on Tomcat

Running Maven project within Eclipse on Tomcat

Here is the simplest way of running a web application that uses Maven as the build tool, from within Eclipse.

This tutorial is based on the following software’s :

1. Java 1.6
2. Eclipse Juno (4.2) JEE Version
3. Maven 2 Eclipse Plugin ( Read this blog to know how to configure M2E Plugin )
4. Maven 3.0.3
5. Simple Hello World web application that uses Struts 2. You can download a simple Hello World app build using Maven from GitHub: Struts Hello World App

Assuming that Eclipse is pointed to a new work space and you have installed and configured M2E plugin correctly, here is what you need to do to import the Maven project and then run it on Tomcat:

1. Click on File -> Import -> Maven -> Existing Maven Project
Maven_ProjectImport_Step1

2. Browse to the location where the Maven project is located. Assuming you have downloaded the Struts2HelloWorld app from GitHub, you would need to browse to /path/Struts2HelloWorld folder, where the Maven pom.xml file is located
3. Eclipse will scan and show the pom.xml file for the Maven project.

4. Click on the check-box for the pom.xml file and import the project (this could take a bit of time, depending on the # of dependencies in the pom file being imported )

Maven_ProjectImport_Step2

5. Maven project should be imported successfully, with no errors. When you look under the project, you should see the ‘Maven Dependencies’ option, which will be pointing to jars from the local maven repository and all these are based on the Maven dependencies mentioned in the pom.xml file

Maven_ProjectImport_Success

6. Right click on the pom.xml file and go to Run As -> Run Configuration
7. In the Run Configuration window, click on Maven Build and create a New Configuration
8. Give the configuration a Name, then select the Base Directory to be the current Maven Project
9. In the Goals, enter ‘clean package’ (without the quotes)

Maven_CleanPackage

10. Apply and Run the configuration. This will basically run the clean and then the package life-cycle of Maven for the current project and you will see the complete Maven output in the Console.
11. Once again, right click on the pom.xml file and go to Run As -> Run Configuration
12. In the Run Configuration window, click on Maven Build and create a New Configuration
13. Give the configuration a different Name, then select the Base Directory to be the current Maven Project
14. In the Goals, enter ‘tomcat:run’ (without the quotes)

Maven_TomcatRun

15. Apply and Run the configuration. Eclipse and M2E will package the application and launch it on an embedded tomcat, on port 8080, via the Apache Tomcat Maven Plugin. You can learn more about this plugin here

16. If you are using the sample app mentioned in this post, then open a browser and go to http://localhost:8080/Struts2HelloWorld and you should see the sample Struts 2 app working.

This is the simplest way to run a Maven project, within Eclipse on Tomcat.

Let me know if you run into any issues.

3 comments

  1. Thanks for the above detailed description. It helps me alot.

  2. I followed all the procedures mentioned above. But at step number 15, the maven project builds successfully without any errors but in tomcat server
    it does not open the file. it gives Http status 404 error.
    Can you please guide me hoe to deploy that project finally on tomcat?

  3. Good Suggession

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code