这几天在写Java Web页面,开发环境是eclipse+maven+jetty。开发java web最烦的就是改个文件需要重启web server,尽管现在的web server 支持了热部署,不过其实现相当于重启了web server,如果文件多些初始化复杂些,重启的时间也够受的。它不像web server那样需要重启服务,而是动态的加载修改的文件,所以反应速度上要好很多,它除了可以热加载class、jsp文件,也可以是spring、hibernate等配置文件。
1、Jrebel介绍:
我们通常做WEB开发的时候经常会修改java类,或者是工程配置文件,而每次修改后都需要将服务器重新启动,才能显示修改后的效果,而有些服务器 像 weblogic ,jboss 启动的时候非常慢,频繁的启动浪费了大量的时间,降低了开发效率。
Jrebel(JavaRebel) 可以帮助解决频繁的重新启动服务器的问题,从而为开发节约了大量的时间。
Jrebel是一个JVM插件(-javaagent),能够即时重载java claass更改,因此不需要重新部署一个应用或者重启容器,节约发者时间。它是一个Java EE和Java 独立应用的常用解决方案。
Jrebel可以和MyEclipse,tomcat等快速的组装在一起,用语动态部署java文件而不用重启服务器,从而可解决大量的开发时间,提高开发效率。
只要配置了Jrebel,它可以监控你的编译文件存放目录,例如:APP_INF/classes 或者 WEB-INF/classes 。
然后在客户端发送页面请求时,关联到该类时,它会自动比较当前JVM中装载的该类是否有修改,如果有则重新装载该类文件。
可以从jrebel得到JRebel,它不是个免费软件,但有30天的试用期,附件中会提供破解版供使用。
使用说明(仅针对目前我用的框架,其他环境可能略有微调):
1.修改项目的pom.xml文件,将已有的jetty plugin配置修改成: <!-- jetty -->
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.0.RC0</version> <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> <webAppConfig> <contextPath>/${project.artifactId}</contextPath> </webAppConfig> </configuration> </plugin>
2.在<build><plugins>中加入
<plugin> <groupId>org.zeroturnaround</groupId> <artifactId>javarebel-maven-plugin</artifactId> <executions> <execution> <id>generate-rebel-xml</id> <phase>process-resources</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>
3.然后运行mvn javarebel:generate。可以在target/classes路径中看到新生成的rebel.xml
示例如下:
<?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/alderaan/rebel-2_0.xsd"> <classpath> <dir name="C:/hop/haierworkspace/avs/bvs/bvs-app/target/classes"> <include name="com/haier/avs/house/domain/AvsBaseAct.hbm.xml"/> <include name="com/haier/avs/house/domain/AvsBzfCompete.hbm.xml"/> <include name="com/haier/avs/house/domain/AvsBzfProductGroup.hbm.xml"/> <include name="com/haier/avs/house/domain/AvsBzfProductProgram.hbm.xml"/> <include name="com/haier/avs/house/domain/AvsBzfProject.hbm.xml"/> <include name="com/haier/avs/house/domain/AvsBzfProjectLog.hbm.xml"/> <include name="com/haier/avs/house/domain/AvsBzfProjectStatus.hbm.xml"/> <include name="com/haier/avs/house/domain/AvsBzfUserRequire.hbm.xml"/> <include name="com/haier/avs/house/domain/AvsMdRegionBranch.hbm.xml"/> <include name="com/haier/avs/house/domain/House-dynamic.xml"/> <include name="com/haier/avs/house/domain/UserRequire-dynamic.xml"/> <include name="com/haier/bvs/console/quartz/domain/QuartzCronTrigger.hbm.xml"/> <include name="com/haier/bvs/console/quartz/domain/QuartzFiredTrigger.hbm.xml"/> <include name="com/haier/bvs/console/quartz/domain/QuartzJobDetail.hbm.xml"/> <include name="com/haier/bvs/console/quartz/domain/QuartzSchedulerState.hbm.xml"/> <include name="com/haier/bvs/console/quartz/domain/QuartzSimpleTrigger.hbm.xml"/> <include name="com/haier/bvs/console/quartz/domain/QuartzTrigger.hbm.xml"/> <include name="com/haier/bvs/util/domain/Common-dynamic.xml"/> <include name="com/haier/bvs/util/domain/SysLog.hbm.xml"/> <include name="com/haier/openplatform/showcase/basic/domain/Department.hbm.xml"/> <include name="com/haier/openplatform/showcase/basic/webapp/action/CreateDepartmentAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/domain/Resource-dynamic.xml"/> <include name="com/haier/openplatform/showcase/security/domain/Resource.hbm.xml"/> <include name="com/haier/openplatform/showcase/security/domain/Role.hbm.xml"/> <include name="com/haier/openplatform/showcase/security/domain/User.hbm.xml"/> <include name="com/haier/openplatform/showcase/security/domain/UserDepartment-dynamic.xml"/> <include name="com/haier/openplatform/showcase/security/service/impl/UserServiceImpl_en_US.properties"/> <include name="com/haier/openplatform/showcase/security/service/impl/UserServiceImpl_zh_CN.properties"/> <include name="com/haier/openplatform/showcase/security/webapp/action/CreateResourceAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/CreateRoleAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/CreateUserAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/LoginAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/ResetPasswordAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/ResetPasswordAction_en_US.properties"/> <include name="com/haier/openplatform/showcase/security/webapp/action/ResetPasswordAction_zh_CN.properties"/> <include name="com/haier/openplatform/showcase/security/webapp/action/RetrievePasswordAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/RetrieveUpdatePasswordAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/UpdatePasswordAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/UpdatePasswordAction_en_US.properties"/> <include name="com/haier/openplatform/showcase/security/webapp/action/UpdatePasswordAction_zh_CN.properties"/> <include name="com/haier/openplatform/showcase/security/webapp/action/UpdateResourceAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/UpdateRoleAction-validation.xml"/> <include name="com/haier/openplatform/showcase/security/webapp/action/UpdateUserAction-validation.xml"/> <include name="env.properties"/> <include name="log4j.xml"/> <include name="spring/avs/house/spring-house.xml"/> <include name="spring/basic/spring-department.xml"/> <include name="spring/cache/ehcache.xml"/> <include name="spring/cache/spring-cache-security.xml"/> <include name="spring/cache/spring-cache.xml"/> <include name="spring/delete-spring-quartz.xml"/> <include name="spring/logutil/spring-logutil.xml"/> <include name="spring/quartz/delete-spring-quartz.xml"/> <include name="spring/security/spring-resource.xml"/> <include name="spring/security/spring-role.xml"/> <include name="spring/security/spring-user.xml"/> <include name="spring/security/spring-userDepartment.xml"/> <include name="spring/spring-common.xml"/> <include name="spring/spring-datasource.xml"/> <include name="spring/spring-external.xml"/> <include name="spring/spring-monitor.xml"/> <include name="spring/spring-transaction.xml"/> <include name="struts/avs/house/struts-house.xml"/> <include name="struts/avs/house/struts-house.xml.tmp"/> <include name="struts/basic/struts-basic.xml"/> <include name="struts/message.properties"/> <include name="struts/quartz/struts-quartz.xml"/> <include name="struts/security/struts-security.xml"/> <include name="struts/struts.xml"/> <include name="xwork-conversion.properties"/> </dir> <dir name="C:/hop/haierworkspace/avs/bvs/bvs-app/target/classes"> </dir> </classpath> <web> <link target="/"> <dir name="C:/hop/haierworkspace/avs/bvs/bvs-app/src/main/webapp"> </dir> </link> </web> </application>
4.配置jetty:run的jvm参数
这是为jetty加载jrebel的关键。
在mvn jetty:run的JRE管理选项卡中的VM参数中加入
-noverify -javaagent:d:/jrebel/jrebel.jar
5:注意关闭eclipse自带的的扫描重启加载:
或者: