简体   繁体   中英

How to run a Maven Project In Tomcat From Eclipse

I am running a multi level maven project. It has webapps packaged as WAR, which depend on components packaged as JAR files

Most of the code changes that i do belong to those JAR files.

To test the changes, i need to repackage the JAR and the WAR and deploy externally in tomcat.

I tried using sysdeo-tomcat-maven-eclipse plugin, but it did not work as expected.

Please advise if anyone has a solution for this problem

Thanks Gaurav

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.

导入模块

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

选择要包含为子项目的依赖项

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).

选择您喜欢的方面

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.

安装部署程序集

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 .

在此输入图像描述

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.

You get 'Unable to read TLD "META-INF/c.tld" from JAR file "file:..g/workspace/.metadata/.plugins' error with the current version of m2eclipse plugin with the above configuration.

You need to delete servlet-api.jar and jsp-api.jar from the deployed projects lib/ folder to fix the above error, and setup custom build configuration so that the 2 files are not copied to the deployment directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM