Maven自动部署到Tomcat

Maven的自动部署可以快速的将编写的maven工程自动部署到远程Tomcat服务器上,并且不需要重启Tomcat容器

步骤

  1. 配置TomcatManager访问权限
    想要自动部署web项目,需要经过Tomcat中Manager,因此需要设置M必须要设置Tomcat的访问权限,就是为了保证项目的安全性。在Tomat官方文档中提到

It would be quite unsafe to ship Tomcat with default settings that allowed anyone on the Internet to execute the Manager application on your server. Therefore, the Manager application is shipped with the requirement that anyone who attempts to use it must authenticate themselves, using a username and password that have one of manager-xxx roles associated with them (the role name depends on what functionality is required). Further, there is no username in the default users file ($CATALINA_BASE/conf/tomcat-users.xml) that is assigned to those roles.

意思是

如果使用默认设置允许网络上任何人在服务器上执行应用程序,则Tomcat是非常不安全的,因此,自带的Manager应用程序要求任何使用该程序的用户都必须拥有manager-xx角色之一的账户。但是在默认的配置文件中并没有开启这些账户,所以在默认情况下是完全禁止访问的。

其中可用的角色分为下面几种:
角色名称|角色可用权限|

manager-gui 访问HTML界面
manager-status 只能访问“服务状态”页面
manager-script 访问纯文本接口和服务状态页面
manager-jmx 访问JMX代理界面和服务状态界面

具体配置如下:

<!-- 文件路径: ${你的tomcat安装路径}/conf/tomcat-users.xml -->

<!--设置角色-->
<role roleName="manager-gui"/>
<role roleName="manager-status"/>
<role roleName="manager-script"/>
<role roleName="manager-jmx"/>

<!-- 添加用户 -->
<user username="填入自己的username" password="填入自己的密码,必须设置" roles="输入角色名称,多个之间用 ',' 分割" />
  1. 配置Manager
    Tomcat在默认安装的情况下已经安装了Manager,但是并没有开启,需要手动将Manager Web的实例通过Context 添加到主机上,那么需要在${tomcat安装路径}/conf/Catalina/localhost下创建一个mananger.xml文件,这个文件用来配置上下文
<? xml version="1.0" encoding="UTF-8">
<Context privileged="true" antiResourceLocking="false"
         docBase="${catalina.home}/webapps/manager">
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.0\.0\.1" />
</Context>
  1. 在pom.xml中添加一下内容
<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<version>2.2</version>
	<configuration>
		<url>http://ip:端口/manager/text</url>
		<username>admin</username>
		<password>123456789</password>
		<path>/webapp</path>
	</configuration>
</plugin>

部分配置解读:

  • url:项目上传Tomcat的地址
  • username: 登录manager的用户名
  • password:登录manager的密码
  • path:当前项目的访问路径
posted @ 2022-04-07 16:55  夏醉浅梦  阅读(112)  评论(0)    收藏  举报