Fix file locking problem with jettyrun

Overview

Whether you are a graphic designer, with little knowledge of computer programming, or a versatile developer building everything all by yourself, you might want at some time to edit static files like stylesheets, hit the refresh button of your browser and check new changes. But you cannot because you realized once the server launched, no static files can be modified.

If you happen to be developing under Windows, you will notice that mvn jetty:run locks files down and prevents editing while the server is running. Super annoying. The problem is that Jetty memory maps files by default and windows locks memory mapped files. SBT users might encounter quite the same situation with the embedded Jetty.

The problem is discussed in slightly more detail here: When using jetty:run, CSS and JavaScript files are locked – how can I fix this?

Maven on Windows platform

Solution 1

  1. extract webdefault.xml from jetty.jar
     jar -xvf ~/.m2/repository/org/mortbay/jetty/jetty/6.1.16/jetty-6.1.16.jar  org/mortbay/jetty/webapp/webdefault.xml
  2. move to test/resources
    mv org/mortbay/jetty/webapp/webdefault.xml src/test/resources/
  3. change useFileMappedBuffer flag to false in webdefault.xml
    <param-name>useFileMappedBuffer</param-name>
    <param-value>false</param-value>
  4. tell mvn jetty plugin to use that file in your pom.xml:
    <plugin>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>maven-jetty-plugin</artifactId>
      <configuration>
      <webDefaultXml>src/test/resources/webdefault.xml</webDefaultXml>
      ...
  5. feel your productivity elevated

Solution 2

Add this configuration snippet to any web.xml :

<servlet>
    <!-- Override init parameter to avoid nasty -->
    <!-- file locking issue on windows.         -->
    <servlet-name>default</servlet-name>
    <init-param>
        <param-name>useFileMappedBuffer</param-name>
        <param-value>false</param-value>
    </init-param>
</servlet>

This method has one drawback. If you deploy on Tomcat it will complain about default server. Tomcat uses same name. Albeit its easier.

Solution 3

For later versions of Jetty and the Maven Jetty plugin you can instrument jetty directly in pom.xml like so:


<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    ...
    <configuration>
        <webApp>
            <_initParams>
                <org.eclipse.jetty.servlet.Default.useFileMappedBuffer>false</org.eclipse.jetty.servlet.Default.useFileMappedBuffer>
            </_initParams>
        </webApp>
    </configuration>
</plugin>

SBT on Unix

Add the following lines to the SBT definition of your projects, typically located in the /project/build/.scala:

override def managedStyle = ManagedStyle.Maven 
override def jettyWebappPath = webappPath 
override def scanDirectories = Nil