如何为IntelliJ中Gradle+Tomcat插件配置JRebel实现热部署
Hey there! Let's get your JRebel hot-deployment up and running with your Gradle + Tomcat setup. I'll break down exactly where to modify the scripts and even share a more IDE-friendly approach since you're using IntelliJ:
First, the location depends on whether you're using a global Gradle installation or the Gradle Wrapper (the default for most projects):
Windows:
- Global Gradle: Find
bin\gradle.batin your Gradle install directory (e.g.,C:\Gradle\gradle-4.3.1\bin\gradle.bat) - Gradle Wrapper: Look in your project root for
gradlew.bat
Add these lines early in the script (before the Java command is executed):
@rem Set JRebel installation path set REBEL_HOME=C:\path\to\your\jrebel\folder @rem Add JRebel agent to JVM options set JAVA_OPTS="-javaagent:%REBEL_HOME%\jrebel.jar %JAVA_OPTS%"- Global Gradle: Find
Linux/macOS:
- Global Gradle: Find
bin/gradlein your Gradle install directory (e.g.,/usr/local/gradle-4.3.1/bin/gradle) - Gradle Wrapper: Look in your project root for
gradlew
Add these lines at the top of the script:
# Set JRebel installation path export REBEL_HOME=/path/to/your/jrebel/folder # Add JRebel agent to JVM options export JAVA_OPTS="-javaagent:$REBEL_HOME/jrebel.jar $JAVA_OPTS"- Global Gradle: Find
Since you're launching Tomcat directly from IntelliJ, you don't need to mess with global scripts—configure it directly in your run/debug settings for better flexibility:
- Open Run > Edit Configurations in IntelliJ
- Find your Tomcat run configuration (it's tied to the
tomcatRunortomcatWarRunGradle task from the bmuschko plugin) - Switch to the Environment Variables tab:
- Click the
+button, addREBEL_HOMEas the name, and paste your JRebel install path as the value
- Click the
- Switch to the VM options field (under the Configuration tab) and add:
-javaagent:$REBEL_HOME/jrebel.jar - Save the configuration and restart your Tomcat instance
After starting Tomcat, check the console output. You should see logs like:
JRebel: Starting up...
JRebel: Monitoring changes in [path/to/your/src/main/webapp]
If you see these lines, go ahead and modify a file in your webapp folder—refresh your browser, and the changes should show up immediately without a full restart.
Make sure your rebel.xml in build/classes/main correctly points to your webapp directory. A basic valid config looks like this:
<?xml version="1.0" encoding="UTF-8"?> <application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd"> <web> <link target="/"> <dir name="src/main/webapp"/> </link> </web> </application>
内容的提问来源于stack exchange,提问作者Al Phaba

