步骤一:修改pox.xml文件
1.首先将 <packaging>jar</packaging>修改为 <packaging>war</packaging>
2.在dependencies里面添加以下代码:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-legacy</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
步骤二:修改SpringBoot中的启动文件
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.test.context.ActiveProfiles; @Configuration @ComponentScan @EnableAutoConfiguration @SpringBootApplication public class DemoApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
步骤三:也是最重要的步骤,就是修改完后不要运行该项目,会报错的。
因为这个是为打包而设计的方案,如果大家想要运行项目的话,一定要把spring-boot-starter-tomcat中 <scope>provided</scope>注释掉才可以运行,不然肯定会出错,这点大家一定要注意。
正确的操作步骤应该是在IDEA中找到Build--》Build Artifacts--》点击生成war包,这样利用IDEA将SpringBoot的项目打包成war文件的所有步骤就完成了。