Spring Boot项目打包成war包

在pom.xml文件中,将打包方式改为war:

1
<packaging>war</packaging>

然后添加如下的Tomcat依赖配置,覆盖Spring Boot自带的Tomcat依赖:

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<build></build>标签内配置项目名(该配置类似于server.context-path=mrbird):

1
2
3
4
5
6
...
<build>
...
<finalName>mrbird</finalName>
</build>
...

添加启动类ServletInitializer:

1
2
3
4
5
6
7
8
9
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}

其中Application为Spring Boot的启动类。

准备完毕后,运行mvn clean package命令即可在target目录下生产war包:

QQ截图20180315191017.png


TOP