问题
I'm curious does anyone know the details on how frameworks like Grails or Play detect changes in the code and automatically trigger a recompilation without rebooting the app server? Is there something specific to Groovy's compiler or its dynamic nature that allows this to easily happen?
For background, I have a custom code generation stage during my build process that I would love to be able to have similar edit-and-refresh capabilities.
Thanks in advance for any pointers, even if I have to sift through code to get the larger picture.
Edit: I should clarify that I'm not looking to build a Grails plugin so much as understand what it takes to do this in any app in a servlet container. i.e., I'm using Groovy but not Grails.
Edit2: It sounds like Play has a specific DEV mode which enables this hot reloading: http://www.playframework.org/documentation/1.1.1/main#lifecycle
I understand that JRebel performs elaborate class versioning via classloaders, but I'm assuming that web frameworks like Grails or Play aren't taking it to that level.
回答1:
Play does it using the Eclipse JDT to compile code at run-time.
Take a look at the following class, that is used by Play to perform the necessary compilation at run time.
https://github.com/playframework/play/blob/master/framework/src/play/classloading/ApplicationCompiler.java
Also, just to note the difference between the DEV mode and PROD mode in Play. This is a design decision made by the Play developers that once an application is put into Production mode, the classes are compiled when the server starts, and are not checked for hot-reloading. This makes sense because in PROD mode, your code should not really be changing.
In DEV mode, Java files are checked for updates each time a new request is received, and any changed files are automatically recompiled (and errors displayed directly in the browsers). This process is very productive in DEV mode, because you will be changing code often, and the immediate feedback is very powerful.
来源:https://stackoverflow.com/questions/5226746/how-do-grails-or-play-detect-changes-and-hot-reload-classes