1.修改pom.xml文件(4個地方)
①<packaging>war</packaging>將其中的jar該為war
②<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> 修改spring-boot-starter-web排除內置的tomcat
③<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
添加這個依賴,不然在打war包的時候會包ClassNotFound異常,打包失敗,這個點很關鍵
④<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>test</warName>
</configuration>
</plugin>
</plugins>
</build>
修改插件為maven-war-plugin,添加configuration節點,配置war包的名稱,解決資源訪問路徑錯誤問題
2. 修改springboot入口配置
①繼承SpringBootServletInitializer
②重寫configure()@SpringBootApplicatio@EnableCachin@ServletComponentScan
public class TestApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(TestApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
3.idea打war包操作
①點擊右側“Maven Project” --> Lifecycle -->clean,清除原先maven的配置對新打包的影響;
②點擊Lifecycle -->install進行打包,看控制台知道程序運行結束Process finished with exit code 0;
4.按照tomcat部署war包的程序進行正常部署,部署完成后啟動tomcat即可正常訪問項目。
