Maven构建war项目添加版本号

  上午接到一个新的需求,项目的war包打包之后,放在了阿里的OSS上,供其他项目下载更新时使用,但是只有一个项目名,也就是pom的artifactId,预期的结果是要加上一个版本号,能区分出是什么时候打的包。

一、使用Maven自带的属性

<properties>

<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>

</properties>
<!-- war打包插件, 设定war包名称带版本号 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>${project.artifactId}_${maven.build.timestamp}</warName>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<includes>
<include>*.*</include>
<include>**/*.*</include>
</includes>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>

按照以上配置,版本号可以加上,年月日也是正确的,但是时分秒是对不上的,这是时区问题,maven取得是UTC ,如果想使用北京时间GMT+8 则需要插件支持

二、使用插件

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<timestampFormat>yyyyMMddHHmmss</timestampFormat>
</configuration>
<executions>
<execution>
<goals>
<goal>create-timestamp</goal>
</goals>
</execution>
</executions>
<inherited>false</inherited>
</plugin>

在需要使用版本号的地方直接使用${timestamp}

<!-- war打包插件, 设定war包名称带版本号 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>${project.artifactId}_${timestamp}</warName>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<includes>
<include>*.*</include>
<include>**/*.*</include>
</includes>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>

配置完成!打包符合要求。

三、扩展使用-静态资源文件版本控制

在 二 的基础上加自定义属性

<properties>
<project.build.version>${timestamp}</project.build.version>
</properties>

页面中的引用

*.[js|css|img]?v=${project.build.version}

注意:war包插件必须要先开启过滤

<filtering>true</filtering>

这样maven打包的时候,就会将${project.build.version} 替换为时间戳,每次发布版本的时候,也不用去清除浏览器的缓存了。

上一篇:Android - JSON Parser Tutorial


下一篇:c语言命名规则 [转载]