一、springboot+maven工程——打war包步骤如下:
1.修改父pom.xml文件:
<packaging>pom</packaging> <!-- 方式一 Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>7.0.92</version> <scope>provided</scope> </dependency> <!-- 方式一 End --> <!-- 方式二 Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- 方式二 End --> <!-- 方式三 Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- 方式三 End -->
注:在这里将scope属性设置为provided,这样在最终形成的WAR中不会包含这个JAR包,因为Tomcat或Jetty等服务器在运行时将 会提供相关的API类。
2.修改web模块的pom.xml文件:
<packaging>war</packaging>
3.修改启动类方法:
方式一:继承 SpringBootServletInitializer 重写 configure()
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } // 修改如下 @SpringBootApplication public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } }
方式二:创建一个新的类继承 SpringBootServletInitializer 重写 configure()
public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } }
总结:父pom.xml文件的packaging为pom方式,有主方法的模块pom.xml文件的packaging为war方式,其他模块pom.xml的packaging为jar方式。
4.打包:
进入到工程文件夹下,执行 mvn clean package 命令。