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

简介: 原文地址: http://www.blogjava.net/sealyu/archive/2010/01/08/308706.htmlProcedurePrerequisites and AssumptionsStep One - Prepare the...

原文地址: http://www.blogjava.net/sealyu/archive/2010/01/08/308706.html

Procedure

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 <tomcat>/conf/tomcat-users.xml file has the following defined:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="admin" password="admin" roles="admin,manager"/>
</tomcat-users>

In this case, we will 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 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):

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

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:

<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> section so that 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 have added the Tomcat plugin for Maven. Note that the <configuration> section needs to point to the server you defined in settings.xml ('myserver'). The <finalName> and the <path> 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:

mvn tomcat:deploy

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:

mvn tomcat:redeploy

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


目录
相关文章
|
3月前
|
XML 前端开发 Java
SpringMVC入门到实战------2、SpringMVC创建实例Hello SpringMVC(maven+tomcat)
这篇文章是SpringMVC框架的入门教程,详细指导了如何在IDEA中使用Maven和Tomcat创建SpringMVC工程,包括添加依赖、配置web.xml、编写控制器、创建配置文件、配置Tomcat服务器以及进行基本的测试,展示了一个简单的Hello SpringMVC示例。
SpringMVC入门到实战------2、SpringMVC创建实例Hello SpringMVC(maven+tomcat)
|
3月前
|
安全 Java 应用服务中间件
【Azure 应用服务】App Service 默认页面暴露Tomcat版本信息,存在安全风险
【Azure 应用服务】App Service 默认页面暴露Tomcat版本信息,存在安全风险
|
3月前
|
Java 关系型数据库 MySQL
"解锁Java Web传奇之旅:从JDK1.8到Tomcat,再到MariaDB,一场跨越数据库的冒险安装盛宴,挑战你的技术极限!"
【8月更文挑战第19天】在Linux上搭建Java Web应用环境,需安装JDK 1.8、Tomcat及MariaDB。本指南详述了使用apt-get安装OpenJDK 1.8的方法,并验证其版本。接着下载与解压Tomcat至`/usr/local/`目录,并启动服务。最后,通过apt-get安装MariaDB,设置基本安全配置。完成这些步骤后,即可验证各组件的状态,为部署Java Web应用打下基础。
56 1
|
3月前
|
Java 应用服务中间件 Shell
Nginx+Keepalived+Tomcat 实现Web高可用集群
Nginx+Keepalived+Tomcat 实现Web高可用集群
107 0
|
3月前
|
Java 应用服务中间件 Windows
【应用服务 App Service】App Service 中部署Java项目,查看Tomcat配置及上传自定义版本
【应用服务 App Service】App Service 中部署Java项目,查看Tomcat配置及上传自定义版本
|
25天前
|
Java Shell 应用服务中间件
Mac系统下配置环境变量:Javajdk、maven、tomcat 环境变量配置及对应配置文件
这篇文章介绍了如何在Mac系统下配置Java JDK、Maven和Tomcat的环境变量,包括配置文件的选择、解决环境变量在zsh shell中无效的问题、查看和设置系统环境变量的方法,以及JDK和Maven的下载、配置和测试步骤。
1003 1
Mac系统下配置环境变量:Javajdk、maven、tomcat 环境变量配置及对应配置文件
|
3天前
|
Java 持续交付 项目管理
使用Maven进行项目管理:提高Java Web开发的效率
Maven 是一款强大的项目管理和构建自动化工具,广泛应用于Java社区。它通过依赖管理、构建生命周期管理、插件机制和多模块项目支持等功能,简化了项目的构建过程,提高了开发效率。本文将介绍Maven的核心功能及其在Java Web开发中的应用。
13 0
|
1月前
|
Java 应用服务中间件 Maven
idea+maven+tomcat+spring 创建一个jsp项目
这篇文章介绍了如何在IntelliJ IDEA中使用Maven和Tomcat创建一个JSP项目,包括配置Maven依赖、设置Tomcat服务器、编写JSP页面、创建控制器和配置文件,以及项目的运行结果。
131 0
idea+maven+tomcat+spring 创建一个jsp项目
|
29天前
|
Java 应用服务中间件 Apache
浅谈Tomcat和其他WEB容器的区别
Tomcat是一款轻量级的免费开源Web应用服务器,常用于中小型系统及并发访问量适中的场景,尤其适合开发和调试JSP程序。它不仅能处理HTML页面,还充当Servlet和JSP容器。相比之下,物理服务器是指具备处理器、硬盘等硬件设施的服务器,如云服务器,其设计目标是在处理能力、稳定性和安全性等方面提供高标准服务。简言之,Tomcat专注于运行Java应用,而物理服务器则提供基础计算资源。
|
3月前
|
应用服务中间件 Linux 网络安全
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面

热门文章

最新文章

推荐镜像

更多