1. 程式人生 > >IntelliJ IDEA 的 Java 熱部署外掛 JRebel 安裝及使用(轉載)

IntelliJ IDEA 的 Java 熱部署外掛 JRebel 安裝及使用(轉載)

jrebel 外掛介紹:http://plugins.jetbrains.com/plugin/4441-jrebel-for-intellij

原文地址:http://wiki.jikexueyuan.com/project/intellij-idea-tutorial/jrebel-setup.html

官網:https://zeroturnaround.com/software/jrebel/trial/

JRebel 介紹

在 Java Web 開發中, 一般更新了 Java 檔案後要手動重啟 Tomcat 伺服器, 才能生效, 浪費不少生命啊, 自從有了 JRebel 這神器的出現, 不論是更新 class 類還是更新 Spring 配置檔案都能做到立馬生效,大大提高開發效率。

JRebel 安裝

JRebel 的安裝方法有兩種, 一種是直接在 Tomcat 啟動引數上面加上 JRebel 的引數, 另外一種是以外掛的形式裝到 IntelliJ IDEA 上, 比較推薦後者。

第一種安裝方法:在 Tomcat 啟動引數上加引數

首先先介紹第一種安裝方法, 先在硬碟某個位置把 JRebel 解壓出來

然後配置 IntelliJ IDEA 的 Tomcat

enter description here

點 + 號選擇 Tomcat Server -> Local

enter description here

預設顯示如圖

enter description here

幾個關鍵的地方需要注意的, 就是首先要選擇 Deployment

 這個選項卡

enter description here

選擇自己的專案, 建議選擇帶 exploded, 這個相當於改 Tomcat 的 CATALINA_HOME, 效率比較高

enter description here

選擇好後, 刪掉預設的Make, 提高效率

enter description here

接下來返回 Server 選項卡, 會發現多了一項 On frame deactivation, 如果你剛才沒有配置 Deployment 選項卡的話的這項是不會出現的

按如圖所示的來配置, 特別需要注意的是 On 'Update' action 和On frame deactivation 這兩專案一定要選擇 Update classes and resources

, 否則類修改熱部署不生效, 或者第三方模版框架例如 Freemarker 熱部署不生效

enter description here

接下來就是很關鍵的需要引入 JRebel 的地方了, 在 VM options 的最右邊有個箭頭, 點進去

enter description here

Windows 輸入:

-noverify
-agentpath:D:/dev_env/jrebel/jrebel_running/lib/jrebel64.dll

enter description here

Linux 用這個:

-agentpath:/dev_env/jrebel/jrebel_running/lib/libjrebel64.so

enter description here

Mac OS 用這個:

-agentpath:/dev_env/jrebel/jrebel_running/lib/libjrebel64.dylib

enter description here

配置完成, 直接啟動 Tomcat 即可, 不過此方法麻煩, 每次新建專案都要從新配置

第二種安裝方法:使用 IntelliJ IDEA 外掛

接下來介紹使用 IntelliJ IDEA 外掛的方式啟動 JRebel

首先是安裝 JRebel 的外掛, 安裝方法和其他外掛安裝方法一樣, 不過這裡不採用線上安裝, 直接選擇本地安裝, 直接選擇外掛安裝即可

enter description here

安裝好後在設定裡面會多出一項 JRebel 的配置

檢視一下外掛是否有效

enter description here

綠色的 VALID 表示是有效的

在原來執行專案的按鈕邊上會多出兩個綠色的按鈕, 如圖, 前面那個是 Run, 後面那個是 Debug

enter description here

自定義容器啟動

配置 Tomcat 的方法和直接上面說的直接呼叫配置方法一樣, 同樣需要注意的是 On 'Update' action 和 On frame deactivation 這兩專案一定要選擇 Update classes and resources, 唯一不同的是 VM options 這項不需要填, 放空就好 接下來直接啟動專案, 一般選擇後面那個 Debug 按鈕

enter description here

看到 Log 有 JRebel 輸出的版本資訊, 沒有報錯就是表示成功執行了, 隨便改一個類試試吧

Maven 或 Gradle 啟動

開啟 JRebel Panel 視窗,然後勾選專案或模組即可,這時外掛會在 src/main/resources 目錄下生成 rebel.xml 檔案,這裡面指定了外掛檢測熱部署資料夾的路徑

enter description here

enter description here

然後在 Maven projects 或 Gradle projects 視窗,右鍵 Task 使用 JRebel 啟動

enter description here

啟動之後更新資源重新編譯(Ctrl + Shift + F9)即可

JRebel 官網有免費啟用服務,到官網註冊領取,請支援正版: