How to create a Maven web app and deploy to Tomcat - fast-Alibaba Cloud Developer Community

Original address: http://www.blogjava.net/sealyu/archive/2010/01/08/308706.html

Procedure

  • Prerequisites and Assumptions
  • Step One - Prepare the Tomcat Manager application
  • Step Two - Create a New Web App Using Maven
  • Step Three - Define Your Tomcat Server in Maven Settings
  • Step Four - Point Your Pom to Your Tomcat Server
  • Step Five - Build and Deploy the Web App

Prerequisites and Assumptions

  • Maven is already installed
  • A local instance of Tomcat is already installed and configured to run on port 8080
  • Optional - Eclipse is installed

Step One - Prepare the Tomcat Manager application

In order to deploy a web app to your Tomcat server, you will need to ensure that you can access the Tomcat Manager application at:http://localhost:8080/manager/html. Typically, you just need to ensure that your / conf/tomcat-users.xml file has the following defined:

In this case, we will be logging in to the Tomcat Manager app using:

Back to Top

Step Two - Create a New Web App Using Maven

Next, we will use a Maven archetype to generate a new web application project.

Assuming that you will primarily use Eclipse as your IDE, you may wish to start the project within Eclipse. We'll start the project within Eclipse and then use Maven to fill the project in with the proper Maven web app structure.

  • In Eclipse create a new General project. New > Project... > General > Project
  • Project name: "sw" (name it what you wish, but remember to replace 'sw' with your project name in all instances where that appears in this document from here on out.)

The steps above just create a General project in your Eclipse workspace (e.g. a folder with a .project settings file inside of it).

Next, open a command prompt, cd into the 'sw' project directory, and then execute the following Maven command (all on one line):

You project will now have the following structure:

  • SW
    • webapp
      • src
        • main
          • resources
          • webapp
            • WEB-INF
              • web.xml
            • index.jsp
      • pom.xml
    • .project

In Eclipse, right-click on the project and select Refresh to see this stuff within Eclipse. (Note that the .project file will typically not appear within Eclipse, but it is there.)

Back to Top

Step Three - Define Your Tomcat Server in Maven Settings

Open your Maven settings.xml file (e.g. C:"Documents and Settings"Administrator".m2"settings.xml) and add a server 'myserver' with the credentials for logging into the Tomcat Manager application:

Back to Top

Step Four - Point Your Pom to Your Tomcat Server

Open the pom.xml file in the 'sw' project and replace section so that it looks like this:

Here, we have added the Tomcat plugin for Maven. Note that section needs to point to the server you defined in settings.xml ('myserver'). and are used to tell the web context that you want to deploy to. In this case, we'll be able to access our application athttp://localhost:8080/sw.

Back to Top

Step Five - Build and Deploy the Web App

On a command prompt, you can now cd into the sw/webapp directory where the project pom exists. The execute the following command:

If you get a BUILD SUCCESSFUL message, you should then be able to access your web application at http://localhost:8080/sw /.

You should see "Hello World! "in your web browser (from the index.jsp page). Now you're all set and ready to start building the next killer web app!

If you try to deploy again with the same command, you will likely get a FAIL message because the application already exists at the path. For successive deployments, use the following command instead:

Next, you may want to enable log4j logging for your new application; check out How to setup Log4j in a web app - fast.

Please read this disclaimer carefully before you start to use the service. By using the service, you acknowledge that you have agreed to and accepted the content of this disclaimer in full. You may choose not to use the service if you do not agree to this disclaimer. This document is automatically generated based on public content on the Internet captured by Machine Learning Platform for AI. The copyright of the information in this document, such as web pages, images, and data, belongs to their respective author and publisher. Such automatically generated content does not reflect the views or opinions of Alibaba Cloud. It is your responsibility to determine the legality, accuracy, authenticity, practicality, and completeness of the content. We recommend that you consult a professional if you have any doubt in this regard. Alibaba Cloud accepts no responsibility for any consequences on account of your use of the content without verification. If you have feedback or you find that this document uses some content in which you have rights and interests, please contact us through this link: https://www.alibabacloud.com/campaign/contact-us-feedback. We will handle the matter according to relevant regulations.
Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now