SpringBoot2.x打包成war(看这篇就够了)
springboot默认打包成jar,如果想打包成war,则需要做以下三步。
1.修改pom.xml文件
a.将jar改成war
1 <groupId>com.test</groupId> 2 <artifactId>01-springboot-HelloWorld</artifactId> 3 <version>0.0.1-SNAPSHOT</version> 4 <packaging>war</packaging>
b.移除spring-boot-starter-web中嵌入式tomcat插件
1 <!-- Add typical dependencies for a web application --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 <!-- 移除嵌入式tomcat插件 --> 6 <exclusions> 7 <exclusion> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-tomcat</artifactId> 10 </exclusion> 11 </exclusions> 12 </dependency>
c.增加servlet引用
1 <dependency> 2 <groupId>javax.servlet</groupId> 3 <artifactId>javax.servlet-api</artifactId> 4 <scope>provided</scope> 5 </dependency>
2.增加启动类
1 import org.springframework.boot.builder.SpringApplicationBuilder; 2 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 3 4 5 /** 6 * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法 7 */ 8 public class SpringBootStartApplication extends SpringBootServletInitializer { 9 10 @Override 11 protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 12 // 注意这里要指向原先用main方法执行的App启动类 13 return builder.sources(App.class); 14 } 15 16 }
3.使用maven打包
maven打包命令:mvn clean install
本项目的结构图如下: