1. 修改tomcat-users.xml文件
tomcat安装目录下,conf/tomcat-users.xml
<tomcat-users version="1.0" xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd">
<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="******" roles="admin-gui,admin,manager-gui,manager,manager-script"/>
</tomcat-users>
2. 修改maven的settings.xml
maven安装目录下,conf/settings.xml
<servers>
<server>
<id>tomcat8</id>
<username>tomcat</username>
<password>******</password>
</server>
</servers>
此处的<id>对应于pom.xml中的<service>,username和password与tomcat-users.xml中一致。
3. pom.xml文件中添加tomcat插件
<!-- 引入tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- 修改访问路径和端口号 -->
<configuration>
<url>http://localhost:8080/manager/text</url>
<username>tomcat</username>
<password>******</password>
<server>tomcat8</server>
<path>/maven-parent</path>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
4. 运行maven项目,有两种方式
第一种方式,首先先打开tomcat服务(startup.bat)
然后右键项目,Run As -> Maven build
输入clean install tomcat7:redeploy 然后回车即可。
第二种方式,手动clean、install然后运行与传统项目一样,部署到tomcat上,直接运行tomcat。