某些情况下(这种情况一般很少见),使用maven构建项目时,需要一个不重复的序列号,比如说,打包时,包名称以当前构建时间结尾,或者每次生成的jar包中包含唯一的序列号,等等;
这个时候,就用到了buildnumber插件,官方网址:
http://mojo.codehaus.org/buildnumber-maven-plugin/index.html
该插件能按照指定的方案生成序列号;首先引入该插件
<!-- 根据系统时间生成唯一序列号 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.2</version>
</plugin>
常用以下两个目标:
buildnumber:create (基于SCM版本生成)
buildnumber:create-timestamp
(基于系统时间生成)
两个目标都默认绑定在
initialize
生命周期;其中create-timestamp目标是
1.0-beta-5
版本新增;
以下分别介绍:
其参数介绍如下:
| Name | Type | Since | Description |
|---|---|---|---|
| buildNumberPropertiesFileLocation | File | 1.0-beta-2 |
当使用"format"参数,并且"item"参数包含"buildNumber"值时,会创建属性文件;此属性定义文件生成位置;
默认值: ${basedir}/buildNumber.properties |
| buildNumberPropertyName | String | 1.0-beta-1 | 自定义" buildNumber "属性名;默认值: buildNumber |
| doCheck | boolean | 1.0-beta-1 |
若设置为true,会检查文件是否修改,若有修改,则构建失败;
Note that this used to be inverted (skipCheck), but needed to be changed to allow releases to work. This corresponds to 'svn status'. 默认值 : false . |
| doUpdate | boolean | 1.0-beta-1 |
若设置为true,版本号会更新为最新;否则会保持为本地值;
Note that this used to be inverted (skipUpdate), but needed to be changed to allow releases to work. This corresponds to 'svn update'. 默认值 : false . |
| format | String | 1.0-beta-1 | 使用java.text.MessageFormat类格式化信息;和"items"参数一起使用;设置该参数会读取"items"参数 |
| getRevisionOnlyOnce | boolean | 1.0-beta-3 | 若设置为true,在多模块的项目中,只会从SCM获取一次版本号; Default value is : false . |
| items | List | 1.0-beta-1 |
和"format"参数一起使用;填充"format"参数的占位符;
有效值为:"scmVersion", "timestamp", "buildNumber[digits]"; 其中[digits]可选,用于选取指定的序列号; |
| locale | String | 1.0-beta-2 |
该属性使用本地Locale信息格式化date和time.该属性值由
Locale.toString()
方法得到;
默认值 :由 Locale.getDefault() .toString() 方法得到; |
| password | String | 1.0-beta-1 | 连接SCM系统时的密码; |
| providerImplementations | Map | 1.0-beta-3 | SCM具体实现的替代方案;其值表示了SCM URL地址,比如"cvs","svn"; |
| revisionOnScmFailure | String | 1.0-beta-2 | 当执行SCM某些操作失败时,可使用此参数值作为替代方案; |
| scmBranchPropertyName | String | 1.0-beta-4 | 自定义" buildScmBranch "属性名称; Default value is : scmBranch . |
| scmDirectory | File | 1.0-beta- | Local directory to be used to issue SCM actions; Default value is : ${basedir} . |
| shortRevisionLength | int | 1.1 | 版本号长度(仅用于git) |
| timestampFormat | String | 1.0-beta-2 | Apply this java.text.MessageFormat to the timestamp only (as opposed to the format parameter). |
| timestampPropertyName | String | 1.0-beta-1 | 自定义" timestamp "属性名; Default value is : timestamp . |
| useLastCommittedRevision | boolean | 1.0-beta-2 |
whether to retrieve the revision for the last commit, or the last revision of the repository.
Default value is : false . |
| username | String | 1.0-beta-1 | 连接SCM的用户名 |
其有两个可选参数
| Name | Type | Since | Description |
|---|---|---|---|
| timestampFormat | String | 1.0-beta-5 |
使用ava.text.SimpleDateFormat类格式化序列号;默认格式不友好,推荐自定义该参数;
|
| timestampPropertyName | String | 1.0-beta-5 | 自定义属性名;默认属性名称是: timestamp . |
个人认为,使用 create-timestamp 目标就足够了。
有关"format"和"items"参数的使用,例子如下:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<format>At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.</format>
<items>
<item implementation="java.lang.Integer">7</item>
<item>timestamp</item>
<item>a disturbance in the Force</item>
</items>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
其他常用配置有
<configuration>
<format>{0,number}.{1,number}.{2,number}</format>
<items>
<item>buildNumber0</item>
<item>buildNumber1</item>
<item>buildNumber2</item>
</items>
</configuration>
<configuration>
<format>{0,date,yyyy-MM-dd HH:mm:ss}</format>
<items>
<item>timestamp</item>
</items>
</configuration>
产生的
${buildNumber}
值分别如下:
At
12
:
30
PM on
Jul
3
,
2053
,
there was a disturbance
in
the
Force
on planet
7.
2.0
.
3
2005
-
10
-
06
2
:
22
:
55
其他详细信息,请参考官网
http://mojo.codehaus.org/buildnumber-maven-plugin/usage.html
PS:一个有用的实践,自定义属性,将生成的序列号赋值其中,便于其他插件等地方使用;
<properties>
<buildtimestamp>${timestamp}</buildtimestamp>
</properties>

