对于非Maven的web项目,有时候我们想不时常通过打war包、拷贝war包、启动tomcat来运行项目、这时候我们可以通过以下方式来进行配置:

1.1:创建web工程。工程结构如下:

1.2、其中index.jsp的内容如下:

<%@ :针对maven项目,若想让项目也能够自动化部署到tomcat中,需要通过maven自动化部署项目到tomcat中,配置方式:

2.1:第一步:配置tomcat访问权限配置是tomcat安装目录下conf文件夹中的tomcat-user.xml文件中配置,

具体配置如下:

<?xml version='1.0' encoding='utf-8'?>

<!--

Licensed to the Apache Software Foundation (ASF) under one or more

contributor license agreements.  See the NOTICE file distributed with

this work for additional information regarding copyright ownership.

The ASF licenses this file to You under the Apache License, Version 2.0

(the "License"); you may not use this file except in compliance with

the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

-->

<tomcat-users>

<role rolename="admin"/>

<role rolename="manager"/>

<role rolename="manager-gui"/>

<role rolename="manager-script"/>

<user username="admin" password="admin" roles="admin,manager,manager-gui,manager-script"/>

</tomcat-users>

2.2:在maven配置文件中配置tomcatserver

安装过maven的朋友们应该都知道maven的配置文件,找到maven的settings.xml配置文件,找到servers,然后配置tomcatserver,具体配置如下:

内容:

<servers>

<!-- server

| Specifies the authentication information to use when connecting to a particular server, identified by

| a unique name within the system (referred to by the 'id' attribute below).

|

| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are

|       used together.

|

-->

<server>

<id>tomcat</id>

<username>admin</username>

<password>admin</password>

</server>

</servers >

2.3:项目的pom.xml中配置tomcat-maven-plugin插件:

打开项目下的pom.xml配置文件,找到plugins标签,我们需要在那里配置tomcat-maven-plugin插件,从网上搜索了一些配置方法,发现有两种不同的配置,分界点是根据tomcat的版本来区分的,tomcat7需要使用新版本,新版本的groupId由org.codehaus.mojo改为org.apache.tomcat.maven,新版本同时也支持tomcat6,接下来的配置我就使用了新版本的配置,具体参考:

<build>

<plugins>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.0.2</version>

<configuration>

<source>1.5</source>

<target>1.5</target>

<fork>true</fork>

<meminitial>128m</meminitial>

<maxmem>512m</maxmem>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<plugin>

<artifactId>maven-eclipse-plugin</artifactId>

<version>2.5.1</version>

<configuration>

<additionalProjectnatures>

<projectnature>

org.springframework.ide.eclipse.core.springnature

</projectnature>

</additionalProjectnatures>

<additionalBuildcommands>

<buildcommand>

org.springframework.ide.eclipse.core.springbuilder

</buildcommand>

</additionalBuildcommands>

<downloadSources>false</downloadSources>

<downloadJavadocs>false</downloadJavadocs>

<wtpversion>1.5</wtpversion>

</configuration>

</plugin>

<!-- 要加上下面的一句,否则执行:mvn package -Dmaven.test.skip=true的时候会报错 -->

<plugin>

<artifactId>maven-war-plugin</artifactId>

<version>2.1.1</version>

</plugin>

<plugin>

<groupId>org.apache.tomcat.maven</groupId>

<artifactId>tomcat6-maven-plugin</artifactId>

<version>2.0</version>

<configuration>

<url>http://127.0.0.1:8080/manager</url>

<server>tomcat</server>

<username>admin</username>

<password>admin</password>

<!-- <update>true</update> -->

<path>/app-tpl-webapp</path>

</configuration>

</plugin>

</plugins>

</build>

注意:这里的path的值就是最后发布后的文件名称。

Tomcat6的url配置必须为http://localhost:8080/manager 后面不能加html或者text,不然报403错误。

Tomcat7的url配置必须为http://127.0.0.1:8080/manager/text text不能替换为html,不然报403错误。

配置参见:http://portlandgo.blog.163.com/blog/static/218936024201433032857104/

最后进入上面pom.xml所在位置,打开所属pom.xml下的cmd命令行窗口,在命令中输入:mvn tomcat6:redeploy

最后发现:

其它参考网站:http://www.cnblogs.com/xyb930826/p/5725340.html

其它参考内容:http://www.cnblogs.com/AloneSword/p/4100072.html

Maven已经是Java的项目管理标配,如何在JavaEE开发使用Maven调用Web应用,是很多同学关心的问题。本文将介绍,Maven如何介绍Tomcat插件。

Maven Tomcat插件现在主要有两个版本,tomcat-maven-plugin和tomcat7-maven-plugin,使用方式基本相同。

tomcat-maven-plugin 插件官网:http://mojo.codehaus.org/tomcat-maven-plugin/plugin-info.html。

tomcat7-maven-plugin 插件官网:http://tomcat.apache.org/maven-plugin.html。

tomcat-maven-plugin  插件使用

配置

在pom.xm 加入以下xml。

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <path>/wp</path>
                    <port>8080</port>
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/manager/html</url>
                    <server>tomcat6</server>
                </configuration>
            </plugin>

简要说明一下:

path  是访问应用的路径

port 是tomcat 的端口号

uriEncoding URL按UTF-8进行编码,这样就解决了中文参数乱码。

Server指定tomcat名称。

配置就这么简单,基本搞掂,下面看看如何使用。

插件运行

如果Eclipse安装了Maven插件,选 择pom.xml文件,击右键——>选择 Run As——> Maven build 。

如果是第一次运行,会弹出下面对话框。在Goals框加加入以下命令: tomcat:run

这样Tomcat 插件就可以运行。

下面介绍几个常用的Goal

命令

描述

tomcat:deploy

部署一个web war包

tomcat:reload

重新加载web war包

tomcat:start

启动tomcat

tomcat:stop

停止tomcat

tomcat:undeploy

停止一个war包

tomcat:run

启动嵌入式tomcat ,并运行当前项目

tomcat7-maven-plugin 使用

配置

两个插件使用方法基本一样,同样需要在pom.xml引用该插件,需要增加以下配置

            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>9090</port>
                    <path>/mgr</path>
                    <uriEncoding>UTF-8</uriEncoding>
                    <finalName>mgr</finalName>
                    <server>tomcat7</server>
                </configuration>
            </plugin>

具体配置一样。

插件使用

在这里要注意一下,该插件命名方式有些不同,比如启动tomcat,对应的目标命令是: tomcat7:run ,同样,其它命令也是这样,需要更改为:tomcat7:<插件执行点>

OK,配置就这么简单,如果需要在tomcat 跟踪联调,可以用Dubug 方式启动maven命令。如下图

tomcat自动运行磁盘任意位置上的项目、使用Maven对tomcat进行自动部署的更多相关文章

  1. Jenkins自动构建gitlab项目(jenkins+maven+giltlab+tomcat)

    环境准备: System:CentOS 7.3 (最小化安装) JDK: 8u161 (1.8_161) tomcat: 8.5.29 Jenkins: Jenkins 2.107.1 Gitlab: ...

  2. IDEA里面maven项目使用maven插件tomcat启动项目

    1.首先在pom.xml添加tomcat插件依赖: <?xml version="1.0" encoding="UTF-8"?> <proje ...

  3. 解决Maven管理项目update Maven时,jre自动变为1.5

    本文为博主原创,未经允许不得转载: 在搭建一个maven web项目时,项目已经按步骤搭建完好,之后项目上就报了一个错误. 在控制台看到错误提示如下:Dynamic Web Module 3.0 re ...

  4. 一台电脑上配置多个tomcat同时运行

    好使 1 1.配置运行tomcat 首先要配置java的jdk环境,这个就不在写了  不懂去网上查查,这里主要介绍再jdk环境没配置好的情况下 如何配置运行多个tomcat 2.第一个tomcat: ...

  5. 如何再一台电脑上配置多个tomcat同时运行

    1.配置运行tomcat 首先要配置java的jdk环境,这个就不在谢了  不懂去网上查查,这里主要介绍再jdk环境没配置好的情况下 如何配置运行多个tomcat 2.第一个tomcat: 找到&qu ...

  6. 修改Tomcat Connector运行模式,优化Tomcat运行性能

    Tomcat是一个小型的轻量级应用服务器,也是JavaEE开发人员最常用的服务器之一.不过,许多开发人员不知道的是,Tomcat Connector(Tomcat连接器)有bio.nio.apr三种运 ...

  7. 修改 Tomcat Connector运行模式 优化Tomcat运行性能

    omcat是一个小型的轻量级应用服务器,也是JavaEE开发人员最常用的服务器之一.不过,许多开发人员不知道的是,Tomcat Connector(Tomcat连接器)有bio.nio.apr三种运行 ...

  8. Linux部署java和tomcat的运行环境

    Linux部署java和tomcat的运行环境 1.上传下载的jdk的rpm包和tomcat的tar包,我是放到/opt目录了,文件直接去官网下载即可. 2.如果之前安装过其他版本的jdk,最好先现在 ...

  9. Eclipse+Maven+JDK+tomcat搭建java的开发环境

    由于最近有几个同事都在学习java方面的东西,所以我写个博文做下笔记,其中遇到过很多个坑,这里就不多说了 首先,我用的是Eclipse+Maven的组合,用Ecplise是周边java开发的同事用这个 ...

随机推荐

  1. 处处留心皆学问——由“display:inline-block;”导致的间距引发的思考。

    昨天在做一个demo时遇到了一个问题:我有五个li需要并排排列,然后自然而然的我给它们设了display:inline-block;但是,过了很久之后发现,除了我写的样式外,它默认有一个间距,我们都不 ...

  2. Mysql查询小作业

    数据准备drop table if exists class;create table class(    class_no int(2) unsigned zerofill primary key ...

  3. Java数据类型与SQL数据类型的映射

    Java数据类型与SQL数据类型的映射 SQL Data Type Java Data Type char/varchar/longvarchar String numeric/decimal jav ...

  4. [自用]数论和组合计数类数学相关(定理&证明&板子)

    0 写在前面 本文受 NaVi_Awson 的启发,甚至一些地方直接引用,在此说明. 1 数论 1.0 gcd 1.0.0 gcd $gcd(a,b) = gcd(b,a\;mod\;b)$ 证明:设 ...

  5. [Codeforces 919F]A Game With Numbers

    Description 题库链接 两个人 Van♂ 游戏,每人手上各有 \(8\) 张牌,牌上数字均为 \([0,4]\) 之间的数.每个人在自己的回合选自己手牌中数字不为 \(0\) 的一张与对方手 ...

  6. Evensgn 的债务

    问题 A: Evensgn 的债务 大致题意:a欠b5元,b欠c5元,那么最小债务总额为a欠c5元,给你关系,求最小债务总额! 不想说话...一句超级大水题,我居然没读懂!!差点想到网络流了...其实 ...

  7. BZOJ3810: [Coci2015]Stanovi

    3810: [Coci2015]Stanovi Description Input 输入一行,三个整数,n, m, k   Output 输出一个数,表示最小不满意度.   Sample Input ...

  8. Postgres中postmaster代码解析(中)

    今天我们对postmaster的以下细节进行讨论: backend的启动和client的连接请求的认证 客户端取消查询时的处理 接受pg_ctl的shutdown请求进行shutdown处理 2.与前 ...

  9. URLDecoder: Illegal hex characters in escape (%) pattern - For input string

    原因:后台发布文章的时候,内容里面有%,导致后台URLDecoder.decode()转码的时候报错. 看了java.net.URLDecoder的decode()的源码,原来是转码错误. 贴出部分代 ...

  10. 谈谈Python中的decorator装饰器,如何更优雅的重用代码

    众所周知,Python本身有很多优雅的语法,让你能用一行代码写出其他语言很多行代码才能做的事情,比如: 最常用的迭代(eg: for i in range(1,10)), 列表生成式(eg: [ x* ...