IDEA 热部署- 自动编译设置 spring boot 热部署devtools实现

原文:https://www.cnblogs.com/TechSnail/p/7690829.html    &&   https://blog.csdn.net/qq_31293575/article/details/80654132 

扩展:

  https://blog.csdn.net/diaomeng11/article/details/73826564

  https://blog.csdn.net/z15732621582/article/details/79439359  : bean.xml配置的项目

===前言===start==========================================================

spring boot 热部署devtools实现

 

1.devtools

  spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用。

2.项目搭建

  本文是采用IDEA搭建的Spring Boot应用,通过spring-boot-devtools配置,可以支持修改java文件会自动重启程序,一些资源无需触发重启,例如thymeleaf模板文件就可以实时编辑。默认情况下,更改/META-INF/maven,/META-INF/resources ,/resources ,/static ,/public 或/templates下的资源不会触发重启,而是触发livereload。devtools模块包含一个嵌入的livereload服务器,可以在资源变化时用来触发浏览器刷新。浏览器需要在livereload.com下载安装扩展。 例如Chrome浏览器在应用商店安装livereload插件后,在要自动刷新的页面点击对应的图标,启动应用后更新页面内容或者css等都会触发页面自动刷新。

3.livereload

  livereload 通过引入的脚本livereload.js在 livereload 服务和浏览器之间建立了一个 WebSocket 连接。每当监测到文件的变动,livereload 服务就会向浏览器发送一个信号,浏览器收到信号后就刷新页面,实现了实时刷新的效果。每次启动时,需要点击对应的图标,如下图所示。

4.项目代码配置

(1)pom.xml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
< dependency >
    < groupId >org.springframework.boot</ groupId >
    < artifactId >spring-boot-devtools</ artifactId >
    < optional >true</ optional >
</ dependency >
< plugin >
    < groupId >org.springframework.boot</ groupId >
    < artifactId >spring-boot-maven-plugin</ artifactId >
    < configuration >
       < fork >true</ fork >  <!-- 如果没有该配置,devtools不会生效 -->
    </ configuration >
</ plugin >

(2)yml配置

1
2
3
4
5
6
devtools:
   livereload:
     enabled: true #是否支持livereload
     port: 35729
   restart:
     enabled: true #是否支持热部署

1.引入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!-- optional=true, 依赖不会传递, 该项目依赖devtools; 
之后依赖boot项目的项目如果想要使用devtools, 需要重新引入 -->
<optional>true</optional>
</dependency>

===前言===end==========================================================

2.配置文件

#开启或者关闭freemarker和thymeleaf的页面缓存
spring.freemarker.cache=false
spring.thymeleaf.cache=true
spring.devtools.restart.enabled=true
#需要开启热部署的文件目录
spring.devtools.restart.additional-paths=src/main/java
#使用了mybatis好像需要设置,应该没有必要。且生产环境需要移除
#restart.include.mapper=/mapper-[\\w-\\.]+jar
#restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar
#静态文件下不需要重启
#spring.devtools.restart.exclude=static/**,public/**
#spring.devtools.restart.exclude=WEB-INF/**
3.更改idea配置
  1) “File” -> “Settings” -> “Build,Execution,Deplyment” -> “Compiler”,选中打勾 “Build project automatically” 。

  2) 组合键:“Shift+Ctrl+Alt+/” ,选择 “Registry” ,选中打勾 “compiler.automake.allow.when.app.running”

4.Chrome禁用缓存(如果还是无法使用)

  F12或者“Ctrl+Shift+I”,打开开发者工具,“Network” 选项卡下 选中打勾 “Disable Cache(while DevTools is open)”


 

1.devtools

  spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用。

2.项目搭建

  本文是采用IDEA搭建的Spring Boot应用,通过spring-boot-devtools配置,可以支持修改java文件会自动重启程序,一些资源无需触发重启,例如thymeleaf模板文件就可以实时编辑。默认情况下,更改/META-INF/maven,/META-INF/resources ,/resources ,/static ,/public 或/templates下的资源不会触发重启,而是触发livereload。devtools模块包含一个嵌入的livereload服务器,可以在资源变化时用来触发浏览器刷新。浏览器需要在livereload.com下载安装扩展。 例如Chrome浏览器在应用商店安装livereload插件后,在要自动刷新的页面点击对应的图标,启动应用后更新页面内容或者css等都会触发页面自动刷新。

3.livereload

  livereload 通过引入的脚本livereload.js在 livereload 服务和浏览器之间建立了一个 WebSocket 连接。每当监测到文件的变动,livereload 服务就会向浏览器发送一个信号,浏览器收到信号后就刷新页面,实现了实时刷新的效果。每次启动时,需要点击对应的图标,如下图所示。

4.项目代码配置

(1)pom.xml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
< dependency >
    < groupId >org.springframework.boot</ groupId >
    < artifactId >spring-boot-devtools</ artifactId >
    < optional >true</ optional >
</ dependency >
< plugin >
    < groupId >org.springframework.boot</ groupId >
    < artifactId >spring-boot-maven-plugin</ artifactId >
    < configuration >
       < fork >true</ fork >  <!-- 如果没有该配置,devtools不会生效 -->
    </ configuration >
</ plugin >

(2)yml配置

1
2
3
4
5
6
devtools:
   livereload:
     enabled: true #是否支持livereload
     port: 35729
   restart:
     enabled: true #是否支持热部署

猜你喜欢

转载自www.cnblogs.com/hahajava/p/10194530.html