spring boot專案打包成war
1 pom.xml打包方式修改為war
2移除spring boot自帶的tomcat包
3新增serverlet-api(編譯階段需要)
<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>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
4新增啟動類
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; public class start extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // 注意這裡要指向原先用main方法執行的Application啟動類 return builder.sources(ProductApplication.class); } } 5、新增war外掛,並取消其對web.xml 的依賴。
打包war專案,必須指向一個web.xml檔案,沒有就會報錯,web.xml檔案的路徑可配置,如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webXml>webapps\WEB-INF\web.xml</webXml> </configuration> </plugin> 由於spring-boot不需要web.xml檔案,可以加入以下配置
<plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> -