官方文档:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
一:更改程序入口类 Application.java 使其继承SpringBootServletInitializer,并重写configure方法
@SpringBootApplication
public class Application extends SpringBootServletInitializer { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
} public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
} }
二:更改pom.xml
<packaging>war</packaging>
三:确保内置servlet container 不会干涉发布该war包的servlet container,方案是标记内置servlet container 的依赖为provided
<dependencies>
<!-- … -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- … -->
</dependencies>
最后,将打好的war包放到tomcat下即可
打成可以运行jar包
还原上面第一步的操作,在pom.xml中添加maven-plugin
<build>
<plugins> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
运行:Java -jar myapp.jar