版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Qizonghui/article/details/80984324
一篇我们主要介绍了在代码中怎么处理可以做到清理缓存的效果,这篇我们说说在打包的时候加版本号,意义上就是给静态资源加版本号,意思我们每次打包的静态页面都是一个全新的页面,通过这种方式来达到清理缓存的目的;为达目标不择手段吗!
1.maven项目打包增加版本号,利用maven插件:com.google.code.maven-replacer-plugin
这种方式在项目打包时执行,自动在静态文件后追加版本号;不需要修改任何代码;
<properties><!--时间戳格式-->
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<basedir>${basedir}/src/main/webapp</basedir>
<includes>
<include>**/*.html</include>
<include>**/*.jsp</include>
</includes>
<regex>false</regex>
<replacements>
<replacement>
<token>.css"</token>
<value>.css?v=${maven.build.timestamp}"</value>
</replacement>
<replacement>
<token>.css'</token>
<value>.css?v=${maven.build.timestamp}'</value>
</replacement>
<replacement>
<token>.js"</token>
<value>.js?v=${maven.build.timestamp}"</value>
</replacement>
<replacement>
<token>.js'</token>
<value>.js?v=${maven.build.timestamp}'</value>
</replacement>
</replacements>
</configuration>
</plugin>
</plugins>
</build>
2.非maven项目打包使用ant
可使用这个target;如果不知道怎么使用ant打包build.xml可以看我关于jenkins中关于ant打包的介绍;
<target name="finddir" >
<echo>检索文件,进行替换js版本</echo>
<tstamp>
<format property="htmlFiltTime" pattern="yyyyMMddHHmmss" offset="0" unit="minute"/>
</tstamp>
<replace dir="${appdirHtml}" includes="**/*.html" encoding="UTF-8">
<replacefilter token="{version}" value="${htmlFiltTime}"/>
</replace>
</target>