springboot打war包并部署到tomcat的方法
1.指定要打包的格式
<packaging>war</packaging>
2.首先要去掉自带的tomcat
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-tomcat</artifactId> 4 <scope>provided</scope> <!--去掉内嵌tomcat--> 5 </dependency> 6
3.然后指定入口类
1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-maven-plugin</artifactId> 6 <!--使用热部署出现中文乱码解决方案--> 7 <configuration> 8 <fork>true</fork> 9 <!--增加jvm参数--> 10 <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments> 11 <!--指定入口类--> 12 <mainClass>com.example.demo.DemoApplication</mainClass> 13 </configuration> 14 </plugin> 15 </plugins> 16 </build>
4.使用maven package打包

5.等待打包完成后会在target目录里面找到刚刚打包的项目
6.把war包解压到tomcat的webapps目录下
7.修改conf目录下的server.xml 自定义该项目的访问路径
<!--在host标签下加入这个配置-->
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="/" docBase="你的项目名称" reloadable="true" /> </Host>
8.启动tomcat即可访问自己的项目,如果404了可以去tomcat的管理页面看看项目是否运行成功

能看到自己的项目就是运行成功了

最后,如果不需要在tomcat中运行,只需要用java -jar命令运行,那第2、3步都可以不做,直接打包后运行即可

浙公网安备 33010602011771号