How to create a Maven web app and deploy to Tomcat-fast

Source: Internet
Author: User

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 for your Tomcat server, you'll need to ensure so you can access the Tomcat Manager Applica tion at:http://localhost:8080/manager/html. Typically, you just need to ensure that your <tomcat>/conf/tomcat-users.xml file has the following defined:

<?xmlversion=‘1.0‘encoding=‘utf-8‘?>
<tomcat-users>
  <rolerolename="manager"/>
  <rolerolename="admin"/>
  <userusername="admin" password="admin"roles="admin,manager"/>
</tomcat-users>

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

Username Admin
Password Admin

??? Back to Top

Step two-create a New Web App Using Maven

Next, we'll use a Maven archetype to generate a new Web application project.

Assuming that you'll primarily use eclipse as your IDE, your 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 is wish, but remember to replace ' SW ' with your project name in all instances where that Appears in the This document is

The steps above just create a general project in your Eclipse workspace (e.g. a folder with a. Project Settings file Insid E of it).

Next, open a command prompt, CD into the ' SW ' project directory, and then execute the following Maven command Line):

mvn archetype:create
   -DgroupId=com.burlesontech.sw
   -DartifactId=webapp
   -DarchetypeArtifactId=maven-archetype-webapp

You project'll now has the following structure:

    • Sw
      • WebApp
        • Src
          • Main
            • Resources
            • WebApp
              • Web-inf
                • 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 would 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 ' Myserv Er ' with the credentials-logging into the Tomcat Manager application:

<settings>
    <servers>
    <server>
        <id>myserver</id>
        <username>admin</username>
        <password>admin</password>
    </server>
    </servers>
...

??? Back to Top

Step four-point Your Pom to Your Tomcat Server

Open the Pom.xml file in the ' SW ' project and replace the <build> sections so, it looks like this:

<build>
    <finalName>sw</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <configuration>
                     <server>myserver</server>
                     <path>/sw</path>
            </configuration>
        </plugin>
    </plugins>
</build>

Here, we had added the Tomcat plugin for Maven. Note that the <configuration> sections needs to the server defined in Settings.xml (' myserver '). The <finalName> and the <path> is used to tell the web context, the 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:

mvn tomcat:deploy

If you get a BUILD successful message, you should then is 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 your ' re all set and ready to start building the next killer Web app!

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

mvn tomcat:redeploy

Next, want to enable log4j logging for your new application; Check out how to setup log4j in a Web app-fast.

How to create a Maven web app and deploy to Tomcat-fast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.