欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Spring Boot 项目热部署配置

最编程 2024-05-01 11:30:54
...

开发工具:IntelliJ IDEA 2021.3.3
Spring Boot版本:2.6.7

在开发过程中,如果每一次修改代码都要重启项目,实在是一件很浪费时间的事情。好在Spring Boot提供了devtools工具包实现热部署,代码修改后可以自动部署并重新热启动项目。

1. 添加依赖

首先在pom.xml文件中添加devtools依赖。

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<scope>runtime</scope>
	<optional>true</optional>
</dependency>

默认情况下,/META-INF/maven、/META-INF/resources、/resources、/static、/templates、/public这些文件夹下的文件修改后不会使应用重启,但是会重新加载,devtools内嵌了一个LiveReload server,当资源发生改变时,浏览器刷新。

2. IDEA中配置

我们修改了类文件后,IDEA默认不会自动编译,需要修改IDEA的配置。

(1)File --> Settings --> Build, Execution, Deployment --> Compiler --> Build project automatically,如图所示

Build project automatically

(2)File --> Settings --> Advanced Settings --> Allow auto-make to start even if developed application is currently running,如图所示

Allow auto-make to start even if developed application is currently running