You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # spring-boot-demo-war
  2. > 本 demo 主要演示了如何将 Spring Boot 项目打包成传统的 war 包程序。
  3. ## 1.开发步骤
  4. ### 1.1.修改启动类
  5. ```java
  6. @SpringBootApplication
  7. public class WarApplication extends SpringBootServletInitializer {
  8. public static void main(String[] args) {
  9. SpringApplication.run(WarApplication.class, args);
  10. }
  11. /**
  12. * 若需要打成 war 包,则需要写一个类继承 {@link SpringBootServletInitializer} 并重写 {@link SpringBootServletInitializer#configure(SpringApplicationBuilder)}
  13. */
  14. @Override
  15. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  16. return application.sources(WarApplication.class);
  17. }
  18. }
  19. ```
  20. ### 1.2.修改 pom.xml
  21. ```xml
  22. <!-- 若需要打成 war 包,则需要将打包方式改成 war -->
  23. <packaging>war</packaging>
  24. <dependencies>
  25. <!-- 若需要打成 war 包,则需要将 tomcat 引入,scope 设置为 provided -->
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-tomcat</artifactId>
  29. <scope>provided</scope>
  30. </dependency>
  31. </dependencies>
  32. <build>
  33. <finalName>${project.artifactId}</finalName>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. <!--若需要打成 war 包,则需要添加 war 插件,这样在mvn package的时候就可以打包成 war 包-->
  40. <plugin>
  41. <groupId>org.apache.maven.plugins</groupId>
  42. <artifactId>maven-war-plugin</artifactId>
  43. <version>3.3.2</version>
  44. <configuration>
  45. <failOnMissingWebXml>false</failOnMissingWebXml>
  46. </configuration>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. ```
  51. ## 2.参考
  52. - [Spring Boot 官方文档之传统部署方式](https://docs.spring.io/spring-boot/docs/3.0.0-M5/reference/htmlsingle/#howto.traditional-deployment.war)