maven-replacer-plugin 静态资源打包方案js css
2017-09-28 15:57
561 查看
解决问题:
防止浏览器缓存,修改js css后无效,需要强刷。
两种解决方案:
1. 不依赖插件,纯代码实现
1.1 实现拦截处理器:ModelAndViewInterceptor implements HandlerInterceptor
1.2 modelAndView.addObject("resourceVersion", this.getVersion());
1.3
项目启动的时候,生成一个key值为 ResourceVersion 的放入缓存中,此处我放在redis . 这里取出来即可。
1.4 页面处理 : 引入文件处: 域名/static/order.js?${resourceVersion}
2. 依赖插件
项目主页:https://code.google.com/archive/p/maven-replacer-plugin/
项目pom.xml :
项目结构:
使用解释:
1. <useCache>true</useCache>,参见Stack Overflow https://stackoverflow.com/questions/39105557/maven-replacer-plugin-to-replace-tokens-in-build-and-not-source
2. 多文件引入使用<basedir>${build.directory}</basedir>,参见Stack Overflow https://stackoverflow.com/questions/42248547/maven-replacer-plugin-with-multiple-files
3. 通过变量${maven.build.timestamp}获取的时间不是当前时区,故引入插件build-helper-maven-plugin
上面两种都能实现想要的效果,推荐项目中用的第二种,用户可以无感知。
第一种浏览器F12 下可以看到js css文件后面带了小尾巴,类似 js?126985456987 ,
而第二种的话,只有取war包下去看源码才能看到,并且对代码也改动非常小。
防止浏览器缓存,修改js css后无效,需要强刷。
两种解决方案:
1. 不依赖插件,纯代码实现
1.1 实现拦截处理器:ModelAndViewInterceptor implements HandlerInterceptor
1.2 modelAndView.addObject("resourceVersion", this.getVersion());
1.3
private String getVersion() { String version = this.cacheService.getString(SystemManager.getAppName(), "ResourceVersion"); if(StringUtils.isEmpty(version)) { version = String.valueOf(System.currentTimeMillis() / 1000L); this.cacheService.set(SystemManager.getAppName(), "ResourceVersion", version, 2592000); } return version; }
项目启动的时候,生成一个key值为 ResourceVersion 的放入缓存中,此处我放在redis . 这里取出来即可。
1.4 页面处理 : 引入文件处: 域名/static/order.js?${resourceVersion}
2. 依赖插件
项目主页:https://code.google.com/archive/p/maven-replacer-plugin/
项目pom.xml :
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> <configuration> <useCache>true</useCache> </configuration> <executions> <execution> <id>prepare-war</id> <phase>prepare-package</phase> <goals> <goal>exploded</goal> </goals> </execution> </executions> </plugin> <!--此插件用于获取当前时区当前时间--> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>timestamp-property</id> <goals> <goal>timestamp-property</goal> </goals> <configuration> <name>build.time</name> <pattern>MdHHmmssSSS</pattern> <timeZone>GMT+8</timeZone> </configuration> </execution> </executions> </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>${build.directory}</basedir> <includes> <include>${build.finalName}/WEB-INF/views/*.vm</include> <include>${build.finalName}/WEB-INF/views/**/*.vm</include> </includes> <replacements> <replacement> <token>\.css\"</token> <value>.css?v=${build.time}\"</value> </replacement> <replacement> <token>\.css\'</token> <value>.css?v=${build.time}\'</value> </replacement> <replacement> <token>\.js\"</token> <value>.js?v=${build.time}\"</value> </replacement> <replacement> <token>\.js\'</token> <value>.js?v=${build.time}\'</value> </replacement> </replacements> </configuration> </plugin> </plugins> </build>
项目结构:
使用解释:
1. <useCache>true</useCache>,参见Stack Overflow https://stackoverflow.com/questions/39105557/maven-replacer-plugin-to-replace-tokens-in-build-and-not-source
2. 多文件引入使用<basedir>${build.directory}</basedir>,参见Stack Overflow https://stackoverflow.com/questions/42248547/maven-replacer-plugin-with-multiple-files
3. 通过变量${maven.build.timestamp}获取的时间不是当前时区,故引入插件build-helper-maven-plugin
上面两种都能实现想要的效果,推荐项目中用的第二种,用户可以无感知。
第一种浏览器F12 下可以看到js css文件后面带了小尾巴,类似 js?126985456987 ,
而第二种的话,只有取war包下去看源码才能看到,并且对代码也改动非常小。
相关文章推荐
- maven-replacer-plugin 静态资源版本号解决方案(css/js等)
- 使用yuicompressor-maven-plugin插件压缩JS和CSS静态资源
- 用maven搭建springmvc项目时,jsp页面无法加载js或者css等静态资源
- SpringBoot静态资源css,js,img配置方案
- Maven 打包jsp/js/jsp静态资源到 JAR 包中(Servlet3.0模块化特性与Maven结合应用)
- 使用Maven + Jetty时,如何不锁定js css 静态资源
- maven yuicompressor-maven-plugin 打包静态资源文件
- 解决myeclipse maven jetty启动后无法修改静态资源js css文件
- 使用Maven + Jetty时,如何不锁定js css 静态资源
- Maven使用yuicompressor-maven-plugin打包压缩css、js文件
- yuicompressor-maven-plugin插件压缩js和css
- 前端静态资源版本更新与缓存之——通过gulp 在原html文件上自动化添加js、css版本号
- Spring MVC程序中得到静态资源文件css,js,图片 文件的路径
- 前端静态资源版本更新与缓存之——通过gulp 在原html文件上自动化添加js、css版本号
- springMVC下jsp引用外部js,css等静态资源的解决方法
- yuicompressor 与 maven结合,打包,压缩js,css
- 仿淘宝 css,js 等静态资源合并压缩输出的 jsp 脚本 combo.jsp
- 使用yuicompressor-maven-plugin合并压缩js和css
- SpringMVC 静态资源js css img访问问题
- 静态资源打包:一个javescript 的src引用多个文件,一个link引用多个CSS文件