假设我们需要向一些工件(jar,war等)添加内部版本号。 在这里,我想演示buildnumber-maven-plugin的用法。
这篇文章基于:
- http://mojo.codehaus.org/buildnumber-maven-plugin/usage.html
- http://www.site.lalitbhatt.com/maven-build-number-plugin
- http://blog.peterlynch.ca/2009/11/buildnumber-maven-plugin-helpful.html
- http://apollo.ucalgary.ca/tlcprojectswiki/index.php/Public/Project_Versioning_-_Best_Practices#Build_Versioning
我们有一些项目,需要在jar清单文件中包含不基于VCS(SVN,Git,Mercurial等)修订版本的顺序内部版本号。 让我们创建适当的pom.xml文件,并实施一个小型演示以验证结果。
生成Maven项目
$ mvn archetype:generate -DgroupId=org.halyph -DartifactId=buildNoTest\
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
创建pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelversion>4.0.0</modelversion><groupid>org.halyph</groupid><artifactid>buildNoTest</artifactid><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>buildNoTest</name><url>http://maven.apache.org</url><dependencies><dependency><groupid>junit</groupid><artifactid>junit</artifactid><version>3.8.1</version><scope>test</scope></dependency></dependencies><properties><project.build.sourceencoding>UTF-8</project.build.sourceencoding></properties><!-- If you have access to scm then you can place actual url's. Otherwise with <revisionOnScmFailure /> you can give some fake URLs as follows. --><scm><connection>scm:svn:http://none</connection><developerconnection>scm:svn:https://none</developerconnection><url>scm:svn:https://none</url></scm><build><resources><resource><directory>src/main/resources</directory></resource><resource><directory>src/main/filtered-resources</directory><filtering>true</filtering></resource></resources><plugins><plugin><groupid>org.codehaus.mojo</groupid><artifactid>buildnumber-maven-plugin</artifactid><version>1.1</version><executions><execution><phase>generate-resources</phase><goals><goal>create</goal></goals></execution></executions><configuration><!-- doCheck and doUpdate actually talk to repository if it's true,Check would check that there are no local changes. Update would update it --><docheck>false</docheck><doupdate>false</doupdate><!-- This ensures that even if we are not connected to scm than alsotake the version from local .svn file --><revisiononscmfailure><!--Generate sequence build number based on:build number and timestamp --><format>Build: #{0} ({1,date})</format><items><item>buildNumber\d*</item><item>timestamp</item></items></revisiononscmfailure></configuration></plugin><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-jar-plugin</artifactid><version>2.1</version><configuration><archive><!-- will put the entries into META-INF/MANIFEST.MF file --><manifestentries><implementation-version>${project.version}</implementation-version><implementation-build>${buildNumber}</implementation-build></manifestentries></archive></configuration></plugin></plugins></build>
</project>
创建演示应用程序以验证结果
package org.halyph;import java.io.IOException;
import java.util.ResourceBundle;
import java.util.jar.Attributes;
import java.util.jar.Manifest;public class App
{public static void main( String[] args ) throws IOException{System.out.println('Verify Resource bundle' );// Check filtered resources based on generated build numberResourceBundle bundle = ResourceBundle.getBundle( 'build' );String msg = bundle.getString( 'build.message' );System.out.println(msg);System.out.println('\nVerify Generated MANIFEST.MF Properties' );// Check Manifest file based on generated build numberManifest mf = new Manifest();mf.read(Thread.currentThread().getContextClassLoader().getResourceAsStream('META-INF/MANIFEST.MF'));Attributes atts = mf.getMainAttributes();System.out.println('Implementation-Versio: ' + atts.getValue('Implementation-Version'));System.out.println('Implementation-Build: ' + atts.getValue('Implementation-Build'));}
}
多次构建应用程序并运行
$ mvn install
$ java -cp target\buildNoTest-1.0-SNAPSHOT.jar org.halyph.App
Verify Resource bundle
Build: #3 (Jun 27, 2012)Verify Generated MANIFEST.MF Properties
Implementation-Versio: 1.0-SNAPSHOT
Implementation-Build: Build: #3 (Jun 27, 2012)
摘要
- 我们应该通过将伪造的<scm>部分添加到pom.xml中并将<revisionOnScmFailure />添加到buildnumber-maven-plugin <configuration>中,通知buildnumber-maven-plugin我们将不使用版本控制修订作为内部版本号。
- 已实现的自定义内部版本号格式,请参阅buildnumber-maven-plugin <配置> / <格式>和<配置> / <项目>。
- 在jar清单中添加了内部版本号,请参阅maven-jar-plugin pom.xml部分
- 测试生成的内部版本号是否可以正确添加到过滤的资源中
- 创建的src \ main \ filtered-resources \ build.properties文件
build.message=${buildNumber}
- 添加了资源过滤,请参见<resource>标志<filtering> true </ filtering>部分
- 演示应用程序验证jar清单文件中的过滤资源和内部版本号
您可以git clone这个项目https://github.com/halyph/blog-sandbox/tree/master/Maven/blogpost_062712
参考: Maven内部版本号插件–我们的JCG合作伙伴 Orest Ivasiv的示例用法,来自Knowledge Is Everything博客。
翻译自: https://www.javacodegeeks.com/2012/10/maven-build-number-plugin-sample-usage.html