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 3.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # spring-boot-demo-war
  2. > 本 demo 主要演示了如何将 Spring Boot 项目打包成传统的 war 包程序。
  3. ## pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-war</artifactId>
  10. <version>1.0.0-SNAPSHOT</version>
  11. <!-- 若需要打成 war 包,则需要将打包方式改成 war -->
  12. <packaging>war</packaging>
  13. <name>spring-boot-demo-war</name>
  14. <description>Demo project for Spring Boot</description>
  15. <parent>
  16. <groupId>com.xkcoding</groupId>
  17. <artifactId>spring-boot-demo</artifactId>
  18. <version>1.0.0-SNAPSHOT</version>
  19. </parent>
  20. <properties>
  21. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  22. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  23. <java.version>1.8</java.version>
  24. </properties>
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <!-- 若需要打成 war 包,则需要将 tomcat 引入,scope 设置为 provided -->
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-tomcat</artifactId>
  34. <scope>provided</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>
  42. <build>
  43. <finalName>spring-boot-demo-war</finalName>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-maven-plugin</artifactId>
  48. </plugin>
  49. </plugins>
  50. </build>
  51. </project>
  52. ```
  53. ## SpringBootDemoWarApplication.java
  54. ```java
  55. /**
  56. * <p>
  57. * 启动器
  58. * </p>
  59. *
  60. * @author yangkai.shen
  61. * @date Created in 2018-10-30 19:37
  62. */
  63. @SpringBootApplication
  64. public class SpringBootDemoWarApplication extends SpringBootServletInitializer {
  65. public static void main(String[] args) {
  66. SpringApplication.run(SpringBootDemoWarApplication.class, args);
  67. }
  68. /**
  69. * 若需要打成 war 包,则需要写一个类继承 {@link SpringBootServletInitializer} 并重写 {@link SpringBootServletInitializer#configure(SpringApplicationBuilder)}
  70. */
  71. @Override
  72. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  73. return application.sources(SpringBootDemoWarApplication.class);
  74. }
  75. }
  76. ```
  77. ## 参考
  78. https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#howto-create-a-deployable-war-file