为了账号安全,请及时绑定邮箱和手机立即绑定

maven-replacer-plugin 静态资源版本号解决方案(css/js等)

标签:
Html/CSS

1.解决方案

解决问题:
    防止浏览器缓存,修改静态文件(js/css)后无效,需要强刷。

解决方案:
    使用 maven 的 com.google.code.maven-replacer-plugin 插件,
    在项目打包 package 时自动为静态文件追加 xxx.js?v=time 的后缀,
    从而解决浏览器修改后浏览器缓存问题,此插件只会在生成 war 包源码时生效,不需要修改任何代码。

2.原始文件和最终生成效果

原始文件:
<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="${resource!}/js/xxx/xxx.js"></script>打包后:
<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="${resource!}/js/xxx/xxx.js?v=20180316082543"></script>

3.pom.xml 中插件添加

<properties>
    <!-- maven.build.timestamp 默认时间戳格式 -->
    <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>
            <!-- 自动识别到项目target文件夹 -->
            <basedir>${build.directory}</basedir>
            <!-- 替换的文件所在目录规则 -->
            <includes>
                <include>${build.finalName}/WEB-INF/views/*.html</include>
                <include>${build.finalName}/WEB-INF/views/**/*.html</include>
            </includes>
            <replacements>
                <!-- 更改规则,在css/js文件末尾追加?v=时间戳,反斜杠表示字符转义 -->
                <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文件中添加的规则:

<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="${resource!}/js/xxx/xxx.js" type="text/javascript"></script><link href="${resource!}/css/xxx/xxx.css" rel="stylesheet" type="text/css">

原文出处:https://www.cnblogs.com/yclimb/p/9934102.html  

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消