某些情况下(这种情况一般很少见),使用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版本新增;

以下分别介绍:

buildnumber:create

其参数介绍如下:

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 isfalse.
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 isscmBranch.
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 istimestamp.
useLastCommittedRevision boolean 1.0-beta-2 whether to retrieve the revision for the last commit, or the last revision of the repository.
Default value isfalse.
username String 1.0-beta-1 连接SCM的用户名

buildnumber:create-timestamp

其有两个可选参数

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}
值分别如下:

At12:30 PM on Jul3,2053, there was a disturbance in the Force on planet 7.
2.0.3
2005-10-062:22:55

其他详细信息,请参考官网

http://mojo.codehaus.org/buildnumber-maven-plugin/usage.html

PS:一个有用的实践,自定义属性,将生成的序列号赋值其中,便于其他插件等地方使用;

<properties>
<buildtimestamp>${timestamp}</buildtimestamp>
</properties>

Maven插件之buildnumber-maven-plugin的更多相关文章

  1. Spring Boot的Maven插件Spring Boot Maven plugin详解

    Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Mave ...

  2. maven课程 项目管理利器-maven 3-4 eclipse安装maven插件和新建maven项目

    本节主要讲了两个主要内容, 1       eclipse安装maven插件 2 新建maven项目 3 本人实操 1       eclipse安装maven插件 eclipse4.0以上和myec ...

  3. Gradle 1.12用户指南翻译——第五十二章. Maven 插件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见:http://blog.csdn.net/column/details/gradle-translation.html翻译项目请关注Github上 ...

  4. maven 插件之 AutoConfig 工具使用笔记

    AutoConfig 是一款 maven 插件,主要用于 Maven 项目打包使用.在我们的工作中,会将自己写的代码打成 jar 包或者 war 包发布到各种环境上.一般地,不用的环境所使用的数据库. ...

  5. MacOS如何正确配置Idea自带Maven插件的环境变量?(亲测)

    背景 安装了IDEA开发工具,想执行Maven的命令.但是又没有通过自己下载Maven的安装包进行安装,只是想直接使用IDEA自带的Maven插件来执行Maven的各种命令.由于刚开始使用macos对 ...

  6. maven 学习---部署基于Maven的war文件到Tomcat

    在本教程中,我们将学习如何使用Maven的Tomcat插件打包并部署一个WAR文件到Tomcat(Tomcat的6和7. 要用到工具: Maven 3 Tomcat 6.0.37 Tomcat 7.0 ...

  7. 基于idea的maven(一)Maven的安装

    1.Maven前置依赖 检查电脑是是否安装java 2.下载maven 网址 www.apache.org 解压 maven 压缩包, 并创建相应的maven本地仓库的路径. 打开 conf文件夹中 ...

  8. (十三)Maven插件解析运行机制

    这里给大家详细说一下Maven的运行机制,让大家不仅知其然,更知其所以然. 1.插件保存在哪里? 与我们所依赖的构件一样,插件也是基于坐标保存在我们的Maven仓库当中的.在用到插件的时候会先从本地仓 ...

  9. java使用tomcat7的maven插件发布到服务器

    服务器容器使用tomcat7的maven插件,使用该插件不需要使用tomcat服务器.以下示例均以paycore项目为例. 一.使用tomcat7-maven-plugin 首先在主pom(payco ...

  10. MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子

    什么是MyBatis Generator MyBatis Generator (MBG) 是一个Mybatis的代码生成器,可以自动生成一些简单的CRUD(插入,查询,更新,删除)操作代码,model ...

随机推荐

  1. 【linux】常用网站

    Kernel: http://www.kernel.org/ LSB (Linux Standard Base): http://www.linuxbase.org/ ELC(Embedded Lin ...

  2. c++ char_traits模板类的实现!!!

    本人写过与此相关的两篇博客,一个是<cstring>头文件的实现,还有一个是<cwchar>的实现.这里的char_traits模板类在此基础上实现. 为了方便.将源码一起封装 ...

  3. XML学习经验实例总结2

    DTD约束 Book.dtd: <!ELEMENT 书架 (书+)> <!ELEMENT 书 (书名,价格,介绍)> <!ELEMENT 书名 (#PCDATA)> ...

  4. byte为什么要与上0xff(转)

    无意间翻看之间的代码,发现了一段难以理解的代码. byte[] bs = digest.digest(origin.getBytes(Charset.forName(charsetName))) ; ...

  5. java中遍历MAP,嵌套map的几种方法

    java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>();    map.put("us ...

  6. 向大家推荐个android的游戏引擎——cocos2d-x

    最近发现单单用android自带的功能函数来编写游戏,往往有很大的局限性,即耗时长,调试繁琐,没有一定的框架.所以博主发现了游戏引擎这个好东西,游戏引擎所拥有的架构和功能函数,使得游戏的编写更加得心应 ...

  7. win32多线程程序设计笔记(第二章)

    第二章线程的第一次接触,主要讲了如何创建线程以及需要注意的几点. 一.创建线程 与调用函数的过程类似;线程只不过用CreateThread的API将函数封装起来,并产生一个与主程序同时执行的程序来调用 ...

  8. Nubia Z5S 基于官方H207/4.4内核的Mokee4.4.4 RC3.2 (2014.7.31修复呼吸灯(能亮依旧不能呼吸))

    特别感谢 yun3195 和 轻描淡写Yhw  帮忙測试 转帖请务必注明本链接地址: http://blog.csdn.net/syhost/article/details/36444259 此ROM ...

  9. 窗口显示于parent控件上(用到了ManualDock函数)

    procedure TForm1.btn1Click(Sender: TObject); begin with TForm2.Create(self) do begin ManualDock(self ...

  10. Delphi中复制带有String的记录结构时不能使用Move之类的内存操作函数

    请看下面的代码: program TestRecord; {$APPTYPE CONSOLE} uses  SysUtils,  Math; type  TRecordA = record    Na ...