Maven 读取程序版本号的三种方案
方案1
在应用项目中,如果应用程序需要获取当前程序的版本号,可以读取 /META-INF/maven/${groupId}/${artifactId}/pom.properties ,获取 maven 生成的版本信息。当然前提用应用程序在运行时得知道自己的 groupId 和 artifactId,否则无法定位路径。
pom.properties 内容示例
#Created by Apache Maven .5.0version=1.0.4-SNAPSHOTgroupId=com.gitee.l0kmartifactId=facelog-service
这种方法很简单,但也有缺点:
貌似这种方法只能获取 maven 默认定义 ${project.version},无法加入自定义的信息。
方案2
还有一个方案就是直接将版本信息写入 MANIFEST.MF。通过 java.util.jar.Manifest 来读取解析MANIFEST.MF来获取版本号。 如下增加 buildnumber-maven-plugin 插件,并给 maven-jar-plugin 插件指定写入 MANIFEST.MF 的参数。
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>buildnumber-maven-plugin</artifactId><version>1.2</version><executions><execution><goals><goal>create</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.0.2</version><configuration><archive><manifestEntries><!-- 项目版本号 --><Project-Version>${project.version}</Project-Version><!-- buildnumber插件提供的 SCM(git/svn等)版本号 --><Scm-Version>${buildNumber}</Scm-Version><!-- 时间戳 --><Timestamp>${maven.build.timestamp}</Timestamp></manifestEntries></archive></configuration></plugin>
方案3
前面两种方案,都需要将应用程序打成jar包才能读取版本信息。
那么程序在开发调试的时候,并没有生成 pom.properties,和 MANIFEST.MF,也就无法读取版本信息了。
所以另一种思路就是用 template-maven-plugin 插件让 maven 自动生成一个包含版本信息的代码如 Version.java。这样任何时候,程序都能很方便的知道自己的版本号了。
模板
首先需要一个代码模板 Version.java,示例如下:
package net.gdface.facelog.service;public final class Version {/** project version */public static final String VERSION = "${project.version}";/** SCM(git) revision */public static final String SCM_REVISION= "${buildNumber}";/** SCM branch */public static final String SCM_BRANCH = "${scmBranch}";/** build timestamp */public static final String TIMESTAMP ="${buildtimestamp}";}
模板放在 /src/main/java-templates/${package_of_template}/ 下
原本在模板文件中用maven内置变量
${maven.build.timestamp}做时间戳,实际运行并没有被正确替换,不知道原因。所以改为使用buildnumber-maven-plugin插件 (goal create-timestamp)生成的时间戳${buildtimestamp}
插件
然后修改 pom.xml 增加 template-maven-plugin 插件和 buildnumber-maven-plugin 插件
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>buildnumber-maven-plugin</artifactId><version>1.4</version><executions><execution><id>bn1</id><goals><!-- 创建${buildNumber} --><goal>create</goal></goals></execution><execution><id>bn2</id><goals><!-- 创建时间戳${buildtimestamp} --><goal>create-timestamp</goal></goals><configuration><!-- 指定时间戳变量名 --><timestampPropertyName>buildtimestamp</timestampPropertyName><!-- 指定日期格式 --><timestampFormat>yyyy-MM-dd HH:mm:ss</timestampFormat></configuration></execution></executions></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>templating-maven-plugin</artifactId><version>1.0.0</version><executions><execution><id>filter-src</id><goals><goal>filter-sources</goal></goals></execution></executions></plugin>
template-maven-plugin 插件会将 /src/main/java-templates/ 文件夹下的所有模板中的 ${xxx} 占位符都用 maven 中同名的变量替换一遍,生成的 Version.java 在 ${project.build.directory}/generated-sources/${package_of_template} 下,并且该文件夹会自动成为源码文件夹加入编译过程。
Spring Boot 获取版本号
在 application.properties 中添加:version=@project.version@
注意值部分为 @project.version@, 而非 ${project.version} 占位符,主要是为了避免与 Spring 语法冲突,配置好之后,可以通过 @Value 注解的方式直接获取。
@Value("${version}")private String version;
也可以 Properties 代码加载
final Properties properties = new Properties();properties.load(this.getClass().getClassLoader().getResourceAsStream("application.properties"));return properties.getProperty("version");
参考资料
- 《Generate a Version.java file in Maven》
- http://www.mojohaus.org/templating-maven-plugin/filter-sources-mojo.html
本文内容来自:
评论