Build Number Maven Plugin的使用方法
1. Build Number Maven Plugin用于获取svn的版本及分支目录,可以将它们用于应用中
2.先在maven的setting.xml中添加仓库,用于下载该插件:
<pluginRepository>
<id>codehaus</id>
<name>codehaus</name>
<url>http://repository.codehaus.org</url>
</pluginRepository>
3.在pom文件中使用插件,将svn的版本和分支写入META-INFO/MANIFEST.MF中
![](http://alicsd.com/wp-content/uploads/2011/08/buildNumber1.jpg)
4.查看效果:在工程下执行mvn cleaninstall -DskipTests=true,打包后,查看包中的META-INFO/MANIFEST.MF文件:
![](http://alicsd.com/wp-content/uploads/2011/08/buildNumber2.jpg)
5.在程序中引用的方法:
System.out.println("Get source branch and version from META-INF/MANIFEST.MF...");
String appServerHome = arg0.getServletContext().getRealPath("/");
File manifestFile = new File(appServerHome, "META-INF/MANIFEST.MF");
Manifest mf = new Manifest();
try {
mf.read(new FileInputStream(manifestFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Attributes atts = mf.getMainAttributes();
System.out.println("Source Branch: "
+ atts.getValue("Implementation-Branch"));
System.out.println("Source Version: "
+ atts.getValue("Implementation-Version"));
通过控制台,我们看到已经得到了我们想要的版本和分支:
6.参考:
http://mojo.codehaus.org/buildnumber-maven-plugin/
http://apollo.ucalgary.ca/tlcprojectswiki/index.php/Public/Project_Versioning_-_Best_Practices