在开发web项目时,常常修改很少的代码(例如配置文件或java代码)就需要重启服务器,来查看修改后的效果。如果项目比较大,重启一次服务器几十秒,这简直就是噩梦!
好在JRebel这个神器的存在,使得我们可以在修改代码后进行热部署,即不需要重启服务器,就可以查看修改后的效果!
下面记录一下JRebel的安装教程和使用方法。
Eclipse下JRebel的安装和使用
0x0 下载安装Jrebel(模仿大神们的装逼排序方法)
- 打开Eclipse—->Help—->EclipseMarketplace;
- 在搜索框输入jrebel,回车搜索;
- 安装完成后,eclipse右上角会多出一个视图选项,同时Help菜单中也多出一个JRebel的菜单;
- 点击Help—->JRebel—->Activation进入激活界面;
- 这里要输入激活码,然后点击激活按钮,途中按钮和你们的不同是因为我的已经激活。
0x1 激活Jrebel
以下介绍如何获取激活码(免费的哦):
进入jrebel官网:https://my.jrebel.com/
注册账号密码,并登陆;
登陆后,出现下图,点击subscribe订阅
- 用twitter或者facebook进行订阅(需要翻墙)
订阅成功后,点击install and active,就会出现喜闻乐见的激活码:
在eclipse中输入激活码激活,点即右上角的JRebel,切换到Jrebel视图,可以看到下图:
0x2 使用Jrebel
激活完成后,JRebel已经默认配置好了。
现在开始使用JRebel运行项目:
非Maven项目
1. 右键点击你要使用JRebel的项目,会出现JRebelR选项,点击Add JRebel Nature,点击后src下会多出一个配置文件rebel。xml
(选这个这个是为了可以监听你项目中class文件的变化,如果你修改了java代码,生成新的class后会被重新加载)
2. 可以运行项目了,右键项目,点击Run As—->Run Configuration,配置运行参数,然后Run
3. 看控制台,出现以下内容说明JRebel监控成功:
2017-11-24 21:39:52 JRebel: Directory 'C:\Users\hasee\oxygen-workspace\crm01\build\classes' will be monitored for changes.
2017-11-24 21:39:52 JRebel: Directory 'C:\Users\hasee\oxygen-workspace\crm01\WebContent' will be monitored for changes.
2017-11-24 21:39:55 JRebel: Starting logging to file: C:\Users\hasee\.jrebel\jrebel.log
2017-11-24 21:39:55 JRebel:
2017-11-24 21:39:55 JRebel: #############################################################
2017-11-24 21:39:55 JRebel:
2017-11-24 21:39:55 JRebel: JRebel Agent 7.1.2 (201711071615)
2017-11-24 21:39:55 JRebel: (c) Copyright ZeroTurnaround AS, Estonia, Tartu.
2017-11-24 21:39:55 JRebel:
2017-11-24 21:39:55 JRebel: Over the last 2 days JRebel prevented
2017-11-24 21:39:55 JRebel: at least 6 redeploys/restarts saving you about 0.2 hours.
2017-11-24 21:39:55 JRebel:
2017-11-24 21:39:55 JRebel: Licensed to fly fly (using myJRebel).
2017-11-24 21:39:55 JRebel:
2017-11-24 21:39:55 JRebel:
2017-11-24 21:39:55 JRebel: #############################################################
2017-11-24 21:39:55 JRebel:
2017-11-24 21:39:55 JRebel: Monitoring Log4j configuration in 'file:/C:/Users/hasee/oxygen-workspace/crm01/build/classes/log4j.properties'.
十一月 24, 2017 9:39:55 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
21:39:55,852 INFO ContextLoader:304 - Root WebApplicationContext: initialization started
21:39:55,979 INFO XmlWebApplicationContext:583 - Refreshing Root WebApplicationContext: startup date [Fri Nov 24 21:39:55 CST 2017]; root of context hierarchy
2017-11-24 21:39:56 JRebel: Monitoring Spring bean definitions in 'C:\Users\hasee\oxygen-workspace\crm01\build\classes\applicationContext.xml'.
21:39:56,197 INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [applicationContext.xml]
2017-11-24 21:39:56 JRebel: Monitoring properties in 'file:/C:/Users/hasee/oxygen-workspace/crm01/build/classes/druid.properties'.
这里说明class文件和webcontent中的文件会被监视,如果发生改变则会重新部署
这里说明spring配置文件applicationContext.xml和德鲁伊连接池配置文件会被监听
- 测试下,修改一段代码,给Action中加一个方法
/***********************************
* Test JRebel
* @return
***********************************/
@Action(value = "test", results = {
@Result(name = "test", location = "customer_list", type = "redirectAction")
})
public String testJRebel() {
System.out.println("Hello JRebel");
return "test";
}
看到控制台输出:
2017-11-24 21:45:43 JRebel: Reloading class 'com.fly.action.CustomerAction'.
大功告成了
补充
如果安装完成后项目无法启动,报错:
JRebel-JVMTI [FATAL] A fatal error occurred while proccessing the base java classes. The JVM has been shut down...
极有可能是以下原因:
1. eclipse所在路径包含中文!
2. 当前系统用户文件夹包含中文
以后更新Idea下JRebel的配置和使用。