springboot打war包并部署到tomcat的方法


1.指定要打包的格式

<packaging>warpackaging>

2.首先要去掉自带的tomcat

1 <dependency>
2     <groupId>org.springframework.bootgroupId>
3     <artifactId>spring-boot-starter-tomcatartifactId>
4     <scope>providedscope>   
5 dependency>
6                 

3.然后指定入口类

 1 <build>
 2     <plugins>
 3         <plugin>
 4             <groupId>org.springframework.bootgroupId>
 5             <artifactId>spring-boot-maven-pluginartifactId>
 6             
 7             <configuration>
 8                 <fork>truefork>
 9                 
10                 <jvmArguments>-Dfile.encoding=UTF-8jvmArguments>
11                       
12                 <mainClass>com.example.demo.DemoApplicationmainClass>
13             configuration>
14         plugin>
15     plugins>
16 build>

4.使用maven package打包

5.等待打包完成后会在target目录里面找到刚刚打包的项目

6.把war包解压到tomcat的webapps目录下

7.修改conf目录下的server.xml 自定义该项目的访问路径


<
Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">   <Context path="/" docBase="你的项目名称" reloadable="true" /> Host>

8.启动tomcat即可访问自己的项目,如果404了可以去tomcat的管理页面看看项目是否运行成功

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

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