IDEA + Spring Boot + JRebel 热部署不能自动编译的解决方案和启动的几种方式

x33g5p2x  于2021-11-22 转载在 Spring  
字(2.6k)|赞(0)|评价(0)|浏览(1206)

自动编译

修改文件后,焦点离开 IDEA 后,任务栏会有进度提示。

  1. 启用 Module 的 JRebel 自动编译:打开 IDEA 左侧 Sidebar 中的 JRebel Panel(View -> Tool Windows -> JRebel),然后勾选需要的 Module。

Jrebel 插件会自动在 resources 目录底下生成一个 rebel.xml。

  1. 允许 IDEA 自动编译:在 Maintenance(Ctrl + Shift + Alt + /) -> Registry 中勾选 compiler.automake.allow.when.app.running,此时 IDEA 会自动选中 Settings -> Build, Execution, Deployment -> Compiler 中的 Build project automatically

如果更改了 Project Structure 导致不能用 JRebel 启动或者启动后不能自动编译,在 Project Structure 不报错的情况下,删除 rebel.xml,重新勾选即可,如果还是没用,在 Project Structure 不报错的情况下,删除 .idea、.iml、rebel.xml 重新导入项目勾选即可。

方式一 Application 类

Run/Debug Configurations 中修改配置。

Application 类右键选择 Run/Debug with JRebel ‘Application’,或者工具栏运行等。

方式二 bootRun

Maven、Gradle 加载了 org.springframework.boot 插件后,在 Maven、Gradle window 中,找到 spring-boot:run(Module name -> Plugins -> spring-boot) 或者 bootRun(Module name -> Tasks -> application),右键选择 Run/Debug with JRebel ‘Module name’ [spring-boot:run/bootRun]

  • Maven
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. </build>

  • Gradle - groovy
  1. buildscript {
  2. ext {
  3. springBootVersion = "2.0.4.RELEASE"
  4. }
  5. dependencies {
  6. classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
  7. }
  8. }
  9. apply plugin: "org.springframework.boot"
  • Gradle - kts
  1. plugins {
  2. id("org.springframework.boot") version "2.1.1.RELEASE"
  3. }

方式三 本地 Tomcat

使用本地 Tomcat 也可以自动编译,就像非 Spring Boot 项目一样,而且不需要启用 JRebel 自动编译和允许 IDEA 自动编译,还可以 Ctrl + F10 手动重新加载,但是此方式启动慢一丢丢。

  1. 新增启动类
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.builder.SpringApplicationBuilder;
  3. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  4. public class SpringBootInitializer extends SpringBootServletInitializer {
  5. @Override
  6. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  7. return builder.sources(Application.class);
  8. }
  9. }
  1. 添加 war 插件:

直接添加 Artifacts 的话,可能会有些文件没有打包进去导致报错。

  • Maven
  1. <packaging>war</packaging>
  2. <dependency>
  3. ……
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-web</artifactId>
  6. <!-- 移除内置 Tomcat -->
  7. <exclusions>
  8. <exclusion>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-tomcat</artifactId>
  11. </exclusion>
  12. </exclusions>
  13. ……
  14. </dependency>
  • Gradle - groovy
  1. apply plugin: 'war'
  • Gradle - kts
  1. plugins {
  2. war
  3. }
  4. apply(plugin = "war")

添加插件后会在 Project Structure -> Project Settings -> Artifacts 自动生成 Web Application: Archive

  1. 配置本地 Tomcat,工具栏使用 JRebel 启动即可:

参考资料:

把 spring-boot 项目部署到 tomcat 容器中
JRebel 免费激活使用

相关文章