前言
本文是SpringBoot配置热部署的教程,启动热部署之后修改html无需再次重启项目,从而大大提高开发效率。网上教程很多,可能用完了很多还不解决问题,本文步骤简单且教程全面,可以解决问题。
操作步骤
一、pom文件中引入依赖
spring-boot-devtools 最重要的功能就是热部署。它会监听 classpath 下的文件变动,并且会立即重启应用。
<!--热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.0.4.RELEASE</version>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
<scope>true</scope>
</dependency>
二、添加插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<!--支持静态文件热部署-->
<addResources>true</addResources>
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
三、打开file->Settings->Compiler,勾选Build project automatically
四、按住ctrl+shift+alt+/ 选Registry进去吧app.running的勾打上
如果idea版本过新找不到上图配置的话, 新版本的配置被迁移到了下图所示。
五、在你的配置文件里面加上:
#配置需要实时跟新的目录
#热部署
spring:
devtools:
livereload:
enabled: true
restart:
enabled: true #设置开启热部署
additional-paths: resources/**,static/**,templates/** #重启目录
注:有的时候浏览器会有缓存,关闭有的时候不方便,可以ctrl+F5刷新一下即可。