如何将SpringBoot项目打包成war文件

版权声明:来自技术分享,QQ475651575 https://blog.csdn.net/weixin_42045591/article/details/83501772

今天给大家解释一下,如何利用IDEA将SpringBoot的项目打包成war文件。

关于为什么要打包成war文件,我这里就不多介绍了,大家有兴趣的可以自己去Google一下。

下面就给出具体的操作步骤:

步骤一:修改pox.xml文件

1.首先将 <packaging>jar</packaging>修改为 <packaging>war</packaging>

2.在dependencies里面添加以下代码:


   
   
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-legacy</artifactId>
  4. <version> 1.0.2.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>javax.servlet</groupId>
  8. <artifactId>javax.servlet-api</artifactId>
  9. <version> 3.0.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>javax.servlet</groupId>
  13. <artifactId>javax.servlet-api</artifactId>
  14. <version> 3.0.1</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>commons-fileupload</groupId>
  18. <artifactId>commons-fileupload</artifactId>
  19. <version> 1.3.1</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-test</artifactId>
  24. <version> 4.1.4.RELEASE</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-tomcat</artifactId>
  29. <scope>provided</scope>
  30. </dependency>
步骤二:修改SpringBoot中的启动文件


   
   
  1. package example;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.builder.SpringApplicationBuilder;
  6. import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
  7. import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
  8. import org.springframework.boot.web.support.SpringBootServletInitializer;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.ComponentScan;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.context.annotation.Import;
  13. import org.springframework.test.context.ActiveProfiles;
  14. @Configuration
  15. @ComponentScan
  16. @EnableAutoConfiguration
  17. @SpringBootApplication
  18. public class DemoApplication extends SpringBootServletInitializer {
  19. @Override
  20. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  21. return application.sources(DemoApplication.class);
  22. }
  23. public static void main(String[] args) {
  24. SpringApplication.run(DemoApplication.class, args);
  25. }
  26. }
步骤三:也是最重要的步骤,就是修改完后不要运行该项目,会报错的。

因为这个是为打包而设计的方案,如果大家想要运行项目的话,一定要把spring-boot-starter-tomcat中 <scope>provided</scope>注释掉才可以运行,不然肯定会出错,这点大家一定要注意。

正确的操作步骤应该是在IDEA中找到Build--》Build Artifacts--》点击生成war包,这样利用IDEA将SpringBoot的项目打包成war文件的所有步骤就完成了。

猜你喜欢

转载自blog.csdn.net/weixin_42045591/article/details/83501772