如何为buildnumber-maven-plugin生成的build.properties添加自定义字段?
问题:如何通过buildnumber-maven-plugin在build.properties中添加更多SVN信息(如最后提交用户)?
我正在使用buildnumber-maven-plugin将各类版本参数纳入WAR制品,以便在页面上展示。我的pom.xml配置如下:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <id>create-buildmeta</id> <phase>prepare-package</phase> <goals> <goal>create-metadata</goal> </goals> </execution> </executions> <configuration> <doCheck>false</doCheck> <doUpdate>false</doUpdate> <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory> </configuration> </plugin>
该配置生成的build.properties内容示例如下:
#Created by build system. Do not modify #Fri Jan 05 17:10:02 EST 2018 version=2.1.0-SNAPSHOT revision=1234 name=myApp timestamp=1515190202388
我希望在该文件中加入更多SVN信息字段,比如分支最后提交用户等。请问是否可以配置实现,具体方法是什么?
回答
当然可以实现!buildnumber-maven-plugin支持从SVN仓库拉取额外的元数据,并自定义输出到build.properties中的字段,下面是具体的修改方案:
1. 调整插件配置
你可以在原配置的基础上,添加SCM连接配置、SVN提供者实现,以及自定义要输出的属性字段。修改后的完整插件配置如下:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <id>create-buildmeta</id> <phase>prepare-package</phase> <goals> <goal>create-metadata</goal> </goals> </execution> </executions> <configuration> <doCheck>false</doCheck> <doUpdate>false</doUpdate> <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory> <!-- 若你的pom中已经配置了<scm>节点(包含connection/developerConnection),可省略此配置 --> <scmConnectionUrl>scm:svn:http://your-svn-repo-url/branches/your-target-branch</scmConnectionUrl> <!-- 使用javasvn(基于SVNKit),无需本地安装SVN客户端;若用本地客户端可改为<svn>svn</svn> --> <providerImplementations> <svn>javasvn</svn> </providerImplementations> <!-- 自定义输出字段:保留原有字段 + 新增SVN相关信息 --> <properties> <version>${project.version}</version> <revision>${buildNumber}</revision> <name>${project.name}</name> <timestamp>${timestamp}</timestamp> <!-- 新增的SVN字段 --> <lastCommitUser>${scmCommitAuthor}</lastCommitUser> <branch>${scmBranch}</branch> <lastCommitTimestamp>${scmCommitTimestamp}</lastCommitTimestamp> <scmUrl>${scmUrl}</scmUrl> </properties> </configuration> </plugin>
2. 新增字段说明
lastCommitUser:当前SVN分支最后一次提交的用户名branch:构建对应的SVN分支路径lastCommitTimestamp:最后一次提交的毫秒级时间戳scmUrl:当前代码所在的SVN仓库完整URL
3. 验证效果
执行mvn prepare-package命令后,生成的build.properties会包含这些新增字段,示例如下:
#Created by build system. Do not modify #Fri Jan 05 17:10:02 EST 2018 version=2.1.0-SNAPSHOT revision=1234 name=myApp timestamp=1515190202388 lastCommitUser=jane_smith branch=branches/feature-user-profile lastCommitTimestamp=1515188500000 scmUrl=http://your-svn-repo-url/branches/feature-user-profile
注意事项
- 确保构建环境能够正常访问你的SVN仓库,插件需要拉取元数据
- 如果你的pom.xml中已经配置了
<scm>节点,无需手动指定<scmConnectionUrl>,插件会自动读取该配置 - 使用
javasvn提供者不需要本地安装SVN客户端,插件通过内置的SVNKit库访问;如果习惯用本地SVN命令行工具,将<providerImplementations>下的<svn>值改为svn即可
内容的提问来源于stack exchange,提问作者amphibient
相关产品推荐
相关产品推荐

