ben*_*eet 5 java browser-cache maven-plugin fingerprinting maven
我正在尝试找到管理浏览器缓存的最佳解决方案,以在 Java/Maven 项目中重新加载修改后的 JavaScript/CSS 资源。最广泛的解决方案似乎是 Maven 过滤,以在构建时向资源 URL 添加时间戳。例如:
<script type="text/javascript" src="resource.js?v=${maven.build.timestamp}"></script>
Run Code Online (Sandbox Code Playgroud)
但最有效的方法是添加文件的校验和/哈希(也称为指纹)而不是构建日期,以便在每次部署后仅在必要时才重新加载资源。我正在拼命寻找使用 Java 或 Maven 插件正确/通用地实现该模型。
有任何想法吗?
谢谢。
您确实想使用指纹识别与查询参数。查询参数方法并不总是有效,并且大多数代理不会缓存它。更改 URL 或实际文件名效果更好。
以下是我在 Maven、Git、Tomcat、Dojo 项目中处理此问题的方法。我使用http://mojo.codehaus.org/buildnumber-maven-plugin/来获取我的 Git rev。然后在构建 WAR 时使用过滤将值注入到我的 JSP 中。
pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<shortRevisionLength>8</shortRevisionLength>
<revisionOnScmFailure></revisionOnScmFailure>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warName>${project.name}-${project.version}-${buildNumber}</warName>
<webResources>
<resource>
<directory>src/main/webapp/WEB-INF/views/includes</directory>
<targetPath>WEB-INF/views/includes</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
......
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
在我的主要 JSP 中,我有
<script src="${pageContext.request.contextPath}/${buildNumber}/static/js/ckeditor/ckeditor.js"></script>
<script src="${pageContext.request.contextPath}/${buildNumber}/static/js/build/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script>
Run Code Online (Sandbox Code Playgroud)
为了进行重写,我正在使用http://tuckey.org/urlrewrite/。我只有一个简单的规则。
我的第一个过滤器条目 web.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)
urlrewrite.xml
<rule match-type="regex">
<from>^/[0-9A-Za-z_.\-]+/static/(.*)$</from>
<to>/static/$1</to>
</rule>
Run Code Online (Sandbox Code Playgroud)