您的位置:首页 > 运维架构 > Tomcat

maven发布war包到tomcat中

2020-02-02 01:15 555 查看

通过maven打包并发到tomcat中

原理如下:

先在本地将代码打成 war 包,然后调用tomcat的接口 *

${host}/manager/text
接口将war包上传到tomcat的
webapp
*目录下,重启tomcat即可

因此需要以下步骤

  1. 配置tomcat权限,使可以可以通过接口方式传war包
  2. 配置tomcat允许访问的ip地址
  3. 在maven的setting中配置tomcat的用户名密码等信息
  4. 在maven的pom.xml中配置maven的地址以及发布项目名称

第一步:配置tomcat权限,使可以可以通过接口方式传war包

编辑tomcat 配置文件

${Catalina_home}/con/tomcat_users.xml
,配置权限如下:

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="gui" roles="manager-gui"/>
<user username="deploy" password="deploy" roles="manager-gui,manager-script"/>

manager-gui 表示 允许访问html接口(即URL路径为/manager/html/) manager-script 表示 允许访问纯文本接口(即URL路径为/manager/text/)

第二步:配置tomcat允许访问的ip地址

编辑tomcat 配置文件

${Catalina_home}/webapp/manager/META-INF/context.xml
,将context节点下的*
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
*注释掉,结果如下:

<?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.
-->
<Context antiResourceLocking="false" privileged="true" >
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

-->
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

第三步:在maven的setting中配置
tomcat
的用户名密码等信息

编辑 maven 中的setting文件,在servers节点中添加一个server子节点如下:

<servers>
<server>
<id>tomcat-maven</id>
<username>deploy</username>
<password>deploy</password>
</server>
</servers>

第四步:在maven的pom.xml中配置maven的地址以及发布项目名称

编辑 maven 中的pom.xml文件,早plugins中添加plugin节点:

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<server>tomcat-maven</server>
<url>http://30.23.76.63:8080/manager/text</url>  <!--注释:该链接适用的角色为tomcat7中设置的manager-script-->
<path>/${finalName}</path>
</configuration>
</plugin>

server 必须和第三步中id的值保持一致

到目前为止配置工作已经完成,只需打包并发布即可

发布方式:只需要在maven打包命令后加上

tomcat7:redeploy
命令即可,如下:

mvn package -Dmaven.test.skip=true tomcat7:redeploy

转载于:https://my.oschina.net/u/2263364/blog/3085154

  • 点赞
  • 收藏
  • 分享
  • 文章举报
chiduoqing6799 发布了0 篇原创文章 · 获赞 0 · 访问量 121 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: