tomcatcachingjbossauto-versioning

Auto-versioning of static content with JBoss


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 timestamp:

// On application deploy:
long CACHE_BUST = System.currentTimeMillis() / 1000;

// later...
session.setAttribute("cacheBust", CACHE_BUST);

4. ...so that I can use these rules in urlrewrite.xml:

<outbound-rule>
    <from>^/static/(css|js|images)/(.*)$</from>
    <to>%{context-path}/static/%{session-attribute:cacheBust}/$1/$2</to>
</outbound-rule>

<rule>
    <from>^/static/\d{10}/(css|js|images)/(.*)$</from>
    <to>/static/$1/$2</to>
</rule>

Many thanks to Pascal and novice for their help.


Solution

  • Following solution is better suited in production environment as you would be incrementing the version number for each release.

    Approach:

    Steps:

    Points: