侧边栏壁纸
  • 累计撰写 75 篇文章
  • 累计创建 41 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

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

勤为径苦作舟
2018-01-16 / 0 评论 / 0 点赞 / 42 阅读 / 4654 字 / 正在检测是否收录...

自动编译

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

  1. 启用 Module 的 JRebel 自动编译(Spring MVC 需要此步):打开 IDEA 左侧 Sidebar 中的 JRebel Panel(View -> Tool Windows -> JRebel),然后勾选需要的 Module。
    JRebel Panel
    Jrebel 插件会自动在 resources 目录底下生成一个 rebel.xml。

  2. 允许 IDEA 自动编译:在 Maintenance(Ctrl + Shift + Alt + /) -> Registry 中勾选 compiler.automake.allow.when.app.running(IDEA 新版没有此项,请手动修改后一句的设置),此时 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 中修改配置。

IDEA 新版要修改选项(Modifiy options),然后选择**执行“更新”操作时(On 'Update' action)切换出 IDE 时(On frame deactivation)**即可。

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

    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </build>
    

    Maven Debug with JRebel

  • Gradle - groovy

    buildscript {
      ext {
        springBootVersion = "2.0.4.RELEASE"
      }
    
      dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
      }
    }
    
    apply plugin: "org.springframework.boot"
    
  • Gradle - kts

    plugins {
      id("org.springframework.boot") version "2.1.1.RELEASE"
    }
    

    Gradle Debug with JRebel

方式三 本地 Tomcat

使用本地 Tomcat 也可以自动编译,就像非 Spring Boot 项目一样,但是此方式启动慢一丢丢。

  1. 新增启动类

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    public class SpringBootInitializer extends SpringBootServletInitializer {
    
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
      }
    }
    
  2. 添加 war 插件:

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

  • Maven

    <packaging>war</packaging>
    <dependency>
    ……
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 移除内置 Tomcat -->
    <exclusions>
      <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
      </exclusion>
    </exclusions>
    ……
    </dependency> 
    
  • Gradle - groovy

    apply plugin: 'war'
    
  • Gradle - kts

    plugins {
      war
    }
    apply(plugin = "war")
    

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

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

MyBatis Mapper XML 热部署

市场搜索并安装这个插件即可:
JRebel MyBatis Plus Extension

参考资料

把 spring-boot 项目部署到 tomcat 容器中

0

评论区