SpringBoot項目中,利用spring-boot-devtools依賴進行配置熱部署
步驟
1、設(shè)置pom.xml
- dependencies中添加 spring-boot-devtools依賴
- build中添加spring-boot-maven-plugin插件,版本跟springboot版本要一直
<!-- dependencies中添加 spring-boot-devtools依賴 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<!-- build中添加spring-boot-maven-plugin插件,版本跟springboot版本要一直 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.6</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
2、IDEA設(shè)置項目自動構(gòu)建
-
打開IDEA設(shè)置(File -> Settings),找到 【Build,Exection,Deployment】菜單,將下圖中紅色框內(nèi)的全部勾選(簡稱ABCD項)
Settings
3、設(shè)置maintenance
先將光標聚焦到 pom.xml文件中,然后使用快捷鍵
Ctrl + Shift + Alt + /(windows) ,或Shift + Alt + Command + /(mac),打開maintenance面板。-
點擊第一個【Registry...】
maintenance面板 -
勾選 compiler.automake.allow.when.app.running,勾選后點擊[close]保存,最后重啟IDEA
compiler.automake.allow.when.app.running
4、 重啟IDEA,確保設(shè)置生效。
- 重啟idea后,先啟動項目。啟動后,嘗試修改代碼,保存。此時項目會自動重啟。
注意:
- spring-boot-devtools 不需要配置版本號,它會根據(jù)你的SpringBoot版本走。
- spring-boot-maven-plugin 的版本號,需要選擇跟你SpringBoot版本一致。
- 最后一步,勾選 compiler.automake.allow.when.app.running 后需要重啟IDEA。
說明
1、如果開啟熱部署后,發(fā)現(xiàn)idea的cpu使用率很高,建議:
- 先確保 Idea中其他項目,Maven都能正常編譯(我遇到過Idea其中有個項目Maven編譯不通過,就會導(dǎo)致Idea一直重新自動編譯該項目)
- 還是不行,可以將idea中的其他項目從Porject中移除掉,只保留一個需要開發(fā)的項目


