目录
- 1.解决方案
- 2.原始文件和最终生成效果
- 3.pom.xml 中插件添加
- 4.html中 css/js 文件引用规则
1.解决方案
1 2 3 4 5 6 7
| 解决问题: 防止浏览器缓存,修改静态文件(js/css)后无效,需要强刷。
解决方案: 使用 maven 的 com.google.code.maven-replacer-plugin 插件, 在项目打包 package 时自动为静态文件追加 xxx.js?v=time 的后缀, 从而解决浏览器修改后浏览器缓存问题,此插件只会在生成 war 包源码时生效,不需要修改任何代码。
|
2.原始文件和最终生成效果
1 2 3 4 5
| 原始文件: <script src="${resource!}/js/xxx/xxx.js"></script>
打包后: <script src="${resource!}/js/xxx/xxx.js?v=20180316082543"></script>
|
3.pom.xml 中插件添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| <properties> <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format> </properties>
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <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>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/*.html</include> <include>${build.finalName}/WEB-INF/views/**/*.html</include> </includes> <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>
|
4.html中 css/js 文件引用规则
文件引用结尾处,必须是pom.xml文件中添加的规则:
1 2 3
| <script src="${resource!}/js/xxx/xxx.js" type="text/javascript"></script>
<link href="${resource!}/css/xxx/xxx.css" rel="stylesheet" type="text/css">
|