Skip to main content

unlock the static files while jetty is running on windows.

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.

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>

                </configuration>
              ...............

</plugin>


that's all... now we can edit & save the static files while jetty server is running.


Happy coding...





Comments

  1. nice one. It really saves my time when i work on the css and .js files.

    ReplyDelete
  2. I am using jetty 9 on windows7 32 bit. Unfortunately this solution does not work on that combination.

    ReplyDelete

Post a Comment

Popular posts from this blog

Running and debugging jetty maven plugin in Netbeans

Coding with Netbeans will make developer life simple. We don't need search for plugins and install different plugins from different places. Maven projects can opened as directly in Netbeans without any settings. Here is the way to run & debug using maven jetty plugin. We can't add Jetty server as servers in Netbeans. Run project: Right click on project and open the properties. click on "Actions" on left side. On right side there will be list of available actions.  To run jetty can be created a new action or override default "Run Project" Here I am overriding default one, Because I can run the project just by hitting "F6" key click on "Run Project" Enter the value "Execute Goals" as "jetty:run", If you want run as offline add "jetty:run -o" "Active Profiles" can be empty In "set Properties" field we can set like skip test. Here I am skipping test. Done, After setting y...

remove(unset) property in a property file using ANT PropertyFile task

Using PropertyFile task we can edit the property file during ANT build. Edit property value are very easy and can find here details. http://ant.apache.org/manual/Tasks/propertyfile.html but delete a property or comment a property is the tricky one. If we are using latest ant version (1.8.1 or later), we can delete a property like below. <propertyfile file="my.properties" comment="My properties">   < entry  key="propertykey" operation="del"/> < /propertyfile> but the older version that is before 1.8.1 don't have operation called " del ", if we run the command in lower version ant, we will get a error says undefined operation "del".      there is workaround instead deleting a property we can comment that property using ant's replace command. < replace file="sample.properties">                      < replacefilter token="propertykey" value="#propert...

Secure Coding Guidelines for the Java Programming Language

http://www.oracle.com/technetwork/java/seccodeguide-139067.html Secure Coding Guidelines for the Java Programming Language, Version 3.0 Introduction 0 Fundamentals Guideline 0-1 Prefer to have obviously no flaws than no obvious flaws Guideline 0-2 Design APIs to avoid security concerns Guideline 0-3 Avoid duplication Guideline 0-4 Restrict privileges Guideline 0-5 Establish trust boundaries Guideline 0-6 Contain sensitive data Guideline 0-7 Particular data format and API issues Guideline 0-7a Avoid dynamic SQL Guideline 0-7b XML and HTML generation requires care Guideline 0-7c Restrict XML inclusion Guideline 0-7d Take care interpreting untrusted code 1 Accessibility and Extensibility Guideline 1-1 Limit the accessibility of classes,interfaces, methods, and fields Guideline 1-1a Limit the accessibility of packages Guideline 1-1b Isolate unrelated code Guideline 1-2 Limit the extensibility of classes and methods Guideline 1-3 Understand how a su...