对于非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. OpenShift实战(六):OpenShift日志监控EFK

    1.镜像下载 为了防止安装过程中由于镜像下载缓慢导致自动部署失败,所以首先提前下载好EFK镜像. docker pull openshift/origin-logging-fluentd docker ...

  2. java小白设计模式之观察者模式

    观察者模式: 对象之间多对一依赖的一种设计方案,被依赖对象为Subject(一),依赖对象为Observer(多),Subject通知Observer变化直接代码: package com.wz.tw ...

  3. StandardContext

    错误信息: 2014-2-6 21:37:19 org.apache.catalina.startup.HostConfig checkResources信息: Reloading context [ ...

  4. [BZOJ]1005 明明的烦恼(HNOI2008)

    BZOJ的第一页果然还是很多裸题啊,小C陆续划水屯些板子. Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间 ...

  5. bzoj3212 Pku3468 A Simple Problem with Integers 线段树

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2046  So ...

  6. ES6(数值)

    数值处理新增特性: 1.新增方法 2.方法调整 1.二进制和八进制 二进制以 0b 开头,其中B和b都可以:八进制以 0o 开头,其中O和o都可以. 2.判断数值是否有效(有限) NaN为非数字,故为 ...

  7. html文本encode后,js获取参数失败的bug

    html中的空格encodeURIComponent后变成%C2%A0,而js中的空格是'%20',二者无法匹配,所以要进行一次替换

  8. Docker安装tomcat和部署项目

    随着微服务的流行,Docker越来越流行,正如它的理念"Build, Ship, and Run Any App, Anywhere"一样,Docker提供的容器隔离技术使得开发人 ...

  9. hasattr(),getattr(),setattr()的使用

    # 首先你有一个command.py文件,内容如下,这里我们假若它后面还有100个方法 class MyObject(object): def __init__(self): self.x = def ...

  10. 学习C语言第一天!

    整理心得笔记: 1)c语言程序由函数构成,每个函数可以实现一个或多个功能.  2)一个正规程序可以有多个函数,但是有且只有一个主函数.  3)函数只有在被调用的时候才执行,主函数由系统调用执行.  4 ...