maven-replacer-plugin 静态资源打包方案js css
来源:互联网 发布:你的理想是什么 知乎 编辑:程序博客网 时间:2024/05/21 08:55
解决问题:
防止浏览器缓存,修改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 . 这里取出来即可。
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 静态资源打包方案js css
- maven yuicompressor-maven-plugin 打包静态资源文件
- 使用yuicompressor-maven-plugin插件压缩JS和CSS静态资源
- Maven使用yuicompressor-maven-plugin打包压缩css、js文件
- maven-war-plugin根据环境打包资源
- maven 打包时通过yuicompressor-maven-plugin压缩js css配置解决.min.js不能打包问题
- wro4j和maven plugin在编译期间压缩静态资源.
- maven-shade-plugin 打包
- 解决myeclipse maven jetty启动后无法修改静态资源js css文件
- maven Springmvc-jsp页面引用js css images静态资源文件
- 用maven搭建springmvc项目时,jsp页面无法加载js或者css等静态资源
- 使用nodejs压缩js,css静态资源
- Gulp:静态资源(css,js)版本控制
- springboot静态资源css,js,img
- 使用yuicompressor-maven-plugin合并压缩js和css
- 使用yuicompressor-maven-plugin压缩js及css文件
- 使用yuicompressor-maven-plugin压缩js及css文件
- maven打包 排除静态资源依赖node_modules等
- 淡入淡出
- 在三层框架页面中 在弹出的页面调用父页面方法
- Redis学习视频教程
- 初学到jdbc数据库连接
- 【转】【Python学习笔记】正则表达式
- maven-replacer-plugin 静态资源打包方案js css
- Appium工作原理介绍
- Linux 安装jdk8
- 详解TensorBoard如何调参
- 网站登录分析
- 直接插入排序 Python实现
- GIT自动部署
- 给AJAX返回值添加数据
- kafka详细配置