idea springboot 打包 war

 一、pom文件中將項目改成   war

二、添加servlet 依賴

<dependency>
 <groupId>javax.servlet</groupId> 
<artifactId>javax.servlet-api</artifactId> 
<version>3.1.0</version> 
<scope>provided</scope>
 </dependency>

 

三、去除內置tomcat(經實驗,不去除依賴並不會影響,項目的啓動,只不過多餘沒法使用)

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

 

四、去除log衝突

若是你使用日誌插件爲log4j 而不是logbcak(springboot內置默認使用logback,以jar跑時啓動不會出錯,打包war使用外置tomcat時會致使棧溢出,沒法啓動)

<dependency> 
<groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter</artifactId>
 <exclusions> 
    <exclusion> 
        <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-logging</artifactId> 
    </exclusion>
 </exclusions>
 </dependency>            

 

五、重寫SpringBootServletInitializer ,使用servlet初始化

import org.springframework.boot.builder.SpringApplicationBuilder;
 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class SpringBootStartApplication extends SpringBootServletInitializer { 
@Override 
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
 // 注意這裏要指向原先用main方法執行的Application啓動類
 return builder.sources(DemoApplication.class);
 } 
}

六、執行maven 打包 clean package -Dmaven.test.skip=true,若命令行或Terminal執行錯誤,能夠選擇idea添加  run  configuration  執行命令

Terminal

 run configuration

 

 

相關文章
相關標籤/搜索