Alex Rivera | Logout

Auto-versioning of static content with JBoss

Asked 2010-04-13T15:32:43.787
9

As per the Q&A here, I'd like to implement a similar auto-versioning system for a web app running in JBoss 5. Is there anything already out there to do this sort of thing, or will I need to write something myself? To be clear: I am not using PHP.

Not knowing much about PHP, I'm not sure what the Tomcat/JBoss analogs of PHP's .htaccess, etc. are. If I do have to write my own auto-versioning, where would I start? The principle is clear to me - rewriting the URL using the file's timestamp, but I don't know much about URL rewriting with JBoss/Tomcat.


Update:

Combining the approaches recommended by Pascal and novice, here's what I ended up with:

1. Custom <my:script/> and <my:style/> tags, so I wouldn't have to see <c:url/> tags everywhere.

<%@ tag body-content="empty" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="src" required="true" rtexprvalue="true" %>
<script src="<c:url value="${src}" />"></script>

2. Following fairly closely to novice's steps, but mapping UrlRewriteFilter to /* in web.xml:

<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

3. Injecting a CACHE_BUST variable to every new session (more or less...), an application deploy

Edit
Report

2 Answers

3

If you don't want to front your application with Apache HTTPD, then you could use a custom servlet filter or reuse the existing Url Rewrite Filter. This filter is based on Apache's mod_rewrite and offers similar capabilities. In other words, it would allow to implement the same solution than the PHP one of the other answer.


I've seen the URL rewrite filter before. Could you elaborate on how I'd use that? I'm really not clear on how I'd apply a filter to this problem, since I wouldn't exactly be calling a JSP/JSTL function wrapped around each included JS/CSS file, and I have no idea about how to get the date modified from a file that's in a WAR.

Well, the idea was to mimic exactly the "PHP solution" of the answer you linked to (let's call this option 1):

  1. Setup the Url Rewrite Filter to rewrite any request to say /css/my.123456.css into /css/my.css
  2. Implement a Servlet that would get a File object for a given resource inside the WAR and insert File#lastModified() in the returned path to that resource.
  3. Invoke that Servlet from the JSP for the CSS.

Another approach (option 2) would be to append an unique query string to the URL of the static content, e.g. the server startup time:

  1. Put the server startup time in the application scope from a ServletContextListener (say under the key "key").
  2. In you JSP

    <link rel="stylesheet" type="text/css" href="/css/my.css?${key.startupTime}">
    

Pro: no url rewrite stuff anymore. Con: Less optimal (content will be request on restart) but acceptable.

answered 2010-04-19T13:38:03.263
1

We do the following in our web app:

  1. Build process retrieves Subversion repository number and stores it in a property within the web application.
  2. Build process also creates a directory structure for static assets within the WAR that incorporates this revision number: /assets/1234/styles/...
  3. Filter/interceptor puts assets path (including revision number) into all requests as attribute
  4. Jsp templates use this assets path attribute to construct URLS for assets
answered 2010-04-22T16:42:59.520

Your Answer