使用maven项目pom.xml文件配置打包功能和静态资源文件自带版本号功能
作者:闲走天涯
在Maven项目中,通过pom.xml文件配置打包功能,可以控制构建过程,生成可部署的包,同时,为了缓存控制与版本更新,可以在打包时给静态资源文件如JS、CSS添加版本号,这通常通过插件如maven-resources-plugin实现
maven项目pom.xml文件配置打包功能和静态资源文件自带版本号功能
例如:
html文件中引用js文件
<script src="js/jquery-2.0.2.min.js"></script>
打包编译后会变成
<script src="js/jquery-2.0.2.min.js?v=2021-07-09T09:29:42Z"></script>
pom.xml标签内增加
<build>
<plugins>
<!--配置打包 start-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--配置打包 end-->
<!--配置页面文件自带版本号 start-->
<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>
<includes>
<include>${basedir}/target/classes/templates/**/*.html</include>
</includes>
<replacements>
<replacement>
<token>\.js\"</token>
<value>.js?v=${maven.build.timestamp}\"</value>
</replacement>
<replacement>
<token>\.js\'</token>
<value>.js?v=${maven.build.timestamp}\'</value>
</replacement>
<replacement>
<token>\.css\"</token>
<value>.css?v=${maven.build.timestamp}\"</value>
</replacement>
<replacement>
<token>\.css\'</token>
<value>.css?v=${maven.build.timestamp}\'</value>
</replacement>
</replacements>
</configuration>
</plugin>
<!--配置页面文件自带版本号 end-->
</plugins>
</build>maven打包时包含resource静态文件
<build>
<!-- maven打包时包含静态资源文件 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yaml</include>
<include>META-INF/**</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
