Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Maven Project In Tomcat From Eclipse

People also ask

How do I run a 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.


Using the Maven Eclipse Integration plugin

I you are experiencing problems I've found it helpful to try the command line tool mvn for debugging. As a rule of thumb, if it builds without errors command line, then you can run it in Eclipse (but tomcat has problems also, so you might need to clean work directory and clear caches every once in a while).

Other than that I really recommend using the Maven Eclipse Integration plugin! I use the version from Sonatypes plugin repo http://m2eclipse.sonatype.org/sites/m2e. There is a newer integration on the eclipse repo, but it works differently and I have not yet had time to investigate it's uses yet.

If your project is modular, it may help to setup the root project as a simple project (no Java , no Java EE). Then import the sub-projects as simple Java projects (again no Java EE). You can still activate dependency management and run the build targets through the project.

Import modules

You only need to select the submodules required by your targeted project(s) by dependencies.

Select dependencies to include as sub projects

For any project that builds war files activate facets for java ee and configure context root under further configuration available (this can only be done once, why the option is missing in the screenshot, so do it right).

Select your decired facets

Then setup deployment assembly for your facated projects. All sibling projects (active in you workspace) should be added to deployment assembly and you also need to add the build path entry for maven dependencies to the deployment assembly. Once this is set up you can use the Run on server directive to boot your web apps.

Setup deployment assembly

I find it useful to always stay in pure java mode when coding maven projects. Any smart views in Eclipse only obscures what happens behind the scene, you get access to the specific editors by activating facated projects anyway!


I use the eclipse webtools plugin, and run my Tomcat from it. It has a republish method that publishes the jars as well (if not, you just clean the directory and republish).

In the pom file, you should have the following lines so that the webtools will be supported automatically

<build>
    ...
    <plugins>
        <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <configuration>
                <wtpversion>1.5</wtpversion>
                ...
            </configuration>
        </plugin>
        ...
    </plugins>
    ...
</build> 

Using the Maven Eclipse Integration plugin and Web Runner

Setup a facated project (already described in another answer), use the latest Maven Eclipse Integration plugin. This should be located at the update site of your eclipse version under collaboration. In my case Indigo - http://download.eclipse.org/releases/indigo (/Collaboration/Maven Integration for Eclipse).

As an alternative to the built in server which often fails on updating workspace dependencies in the deployment assembly (for tomcat) I would recommend using Sonatype Web Application Launcher Webby which is similar to the maven directive jetty:run of the maven jetty plugin. You can find the plugin at sonatypes update site for the plugin M2e Webby - http://m2eclipse.sonatype.org/sites/m2e-webby/. Once this plugin is installed you can Run configuration and run the web-app from an auto build located in {project}/target/m2e-webby.

enter image description here

I recommend configuring one of the (default) embedded jetty-containers as provider for the servlet-api. If specific runtimes have to be tested, an external container can easily be configured by browsing to a server home directory and selecting the appropriate provider .

The disadvantages I've encountered are quite minor:

  • Web applications must be manually shut down, there is no "restart".
  • Each web application has to run on a separate port, they do not share container instance.