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

本文介绍如何使用 maven 的 com.google.code.maven-replacer-plugin 插件来自动添加版本号,防止浏览器缓存。

 

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

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

 

<properties>
    <!-- maven.build.timestamp 默认时间戳格式 -->
    <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>

<build>
    <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>**/*.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>
</build>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值