When running a java maven web application in jetty on windows. All static files are locked and not able edit and save those files while jetty is running.
..........
<configuration>
<webAppConfig>
<contextPath>/</contextPath>
This will eat developers time beacuse each and every change in javascript & css files, jetty have to re-started. Depends on application's dependencies that build will take more than 5minutes.
Why jetty is locking the static files?
Jetty buffers static content for webapps such as html files, css files, images etc and uses memory mapped files to do this if the NIO connectors are being used. The problem is that on Windows, memory mapping a file causes the file to be locked, so that the file cannot be updated or replaced. This means that effectively you have to stop Jetty in order to update a file
Jetty provides a configuration switch in the webdefault.xml file for the DefaultServlet which enables or disables the use of memory mapped files. If you are running on Windows and are having file locking problems, you should set this switch to disable memory mapped file buffers.
webdefault.xml:
The webdefault.xml file saves web applications from having to define a lot of house-keeping and container-specific elements in their own web.xml files.
Here is the solution to solve that problem:
1. Find the jetty jar in maven repository normally in windows path will be C:\Documents and Settings\<user>\.m2\repository\org\mortbay\jetty\jetty\6.1.25\jetty.jar.
Extract the jar and find the webdefault.xml org/mortbay/jetty/webapp/webdefault.xml
2. Change the value for "useFileMappedBuffer" from "true" to "false" in webdefault.xml
<init-param> <param-name>useFileMappedBuffer</param-name> <param-value>true</param-value> <!-- change to false --> </init-param>
3. copy the webdefault.xml to your applicaiton dierectory "src/main/resources"
4. Edit your POM to specify the custom webdefault.xml in jetty plugin
Add the below in jetty plugin configuration under the tag <WebAppConfig>
<defaultsDescriptor>webdefault.xml</defaultsDescriptor>
<plugin>
<configuration>
<webAppConfig>
<contextPath>/</contextPath>
.................
<defaultsDescriptor>webdefault.xml</defaultsDescriptor>
</webAppConfig>
<defaultsDescriptor>webdefault.xml</defaultsDescriptor>
</webAppConfig>
</configuration>
...............
</plugin>
that's all... now we can edit & save the static files while jetty server is running.
Happy coding...
that's all... now we can edit & save the static files while jetty server is running.
Happy coding...
nice one. It really saves my time when i work on the css and .js files.
ReplyDeleteI am using jetty 9 on windows7 32 bit. Unfortunately this solution does not work on that combination.
ReplyDelete