首先在Tomcat里配置deploy的用户(tomcat根目录/conf/tomcat-users.xml):
<role rolename="tomcat"/> <role rolename="manager"/> <role rolename="manager-gui"/> <role rolename="manager-script" /> <role rolename="admin-gui"/> <user username="tomcat" password="tomcat" roles="tomcat,manager,manager-script,admin-gui" /> <user username="tomcat" password="tomcat" roles="manager-gui" />
2.配置maven setting.xml
修改Maven的setting.xml(默认是C:Users用户名.m2settings.xml),在节点下添加
<server> <id>tomcat7</id> <username>tomcat</username> <password>tomcat</password> </server>
3.配置settings.xml
修改Maven的setting.xml(默认是C:Users用户名.m2settings.xml),在节点下添加
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://127.0.0.1:8080/manager/text</url> <server>tomcat7</server> <path>/ROOT</path> <charset>utf8</charset> <update>true</update> </configuration> </plugin>
finalName>webx_0100_helloworld</finalName> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8080/manager/text</url> <!-- server、username、password对应maven的setting下的配置 --> <server>tomcat7</server> <path>/${project.build.finalName}</path> <!-- war文件路径缺省情况下指向target --> <!--<warFile>${basedir}/target/${project.build.finalName}.war</warFile>--> </configuration> </plugin>
<!-- 添加相关依赖 --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>8.5.4</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 移除嵌入式tomcat插件 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
4.执行部署命令
//这里我要求先重新打包,并跳过测试,再部署
//第一次
mvn package -Pdevelop -Dmaven.skip.test=true tomcat7:deploy
//之后
mvn package -Pdevelop -Dmaven.skip.test=true tomcat7:redeploy
注意:一开始一直报错
[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:
redeploy (default-cli) on project webx_0100_helloworld: Cannot invoke Tomcat man
ager: Connection reset by peer: socket write error -> [Help 1]
- 查找到的会造成这个的原因有:
- Tomcat里的users配置错误,XXX-gui只有页面操作的权限,同时同一个用户不能即是manager-gui,同时是 manager-script 或者 manager-jmx,
参考http://stackoverflow.com/questions/26356477/tomcat-7-connection-reset-by-peer-or-software-caused-connection-abort - pom.xml里的url路径写的有问题,试过
http://localhost:8080/manager/,http://localhost:8080/manager/html 都不行,最后按照其他人的换成 http://localhost:8080/manager/text 才成功