每次修改代码时启动 Spring Boot 程序很浪费时间,如何能够支持热部署呢?
步骤
- 确保 devtools 在依赖中。如果你使用 Spring Boot 的初始化工具添加了dev tools,应该不需要额外的修改。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
- 在 intelli 中修改 commpiler ,勾选上 “Build project automatically”
Preferences | Build, Execution, Deployment | Compiler
补充
- 如果你看到有的资料需要你设置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
根据 https://docs.spring.io/spring-boot/docs/current/maven-plugin/start-mojo.html 的记载,fork 默认会是 true 所以不设置也不会有问题
- 当然可以在命令行下利用热部署进行调试。例如,使用 mvn spring-boot:run 运行程序后,可以另开一个终端窗口运行,运行 mvn package 进行打包。打包完成后,会发现运行 mvn spring-boot:run 的终端会自动重新加载。