軟件項目需要通過version來區分版本,用於系統信息展示,系統版本升級等等。
方式一、通過 getImplementationVersion 的方式
在 IDE 開發環境下,無法 通過 getImplementationVersion() 方法獲取版本,只能從已打jar 包並含有 Manifest 文件的情況下才能獲取版本信息。
以下爲 IoTDB的版本獲取方式
//when running the program in IDE, we can not get the version info using getImplementationVersion() public static final String VERSION = IoTDBConstant.class.getPackage().getImplementationVersion() != null ? IoTDBConstant.class .getPackage().getImplementationVersion() : "UNKNOWN";
方式二、通過配置文件的方式
在配置文件中配置版本,然後在代碼裏獲取
@Value("${version}")
private String appVersion;
考慮到如果通過配置文件的方式獲取,需要額外增加配置文件,每次版本升級都需要修改配置文件,
當其他module 需要依賴本項目並獲取版本的情況下,也無法有效解決獲取版本問題。
方式三、通過插件自動生成代碼
通過 maven-replacer-plugin 插件的方式來處理。
<properties>
<version.template.file>src/main/resources/Version.java.template</version.template.file>
<version.file>src/main/java/org/apache/xxx/db/conf/Version.java</version.file>
</properties>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${version.template.file}</file>
<outputFile>${version.file}</outputFile>
<replacements>
<replacement>
<token>@buildnumber@</token>
<value>${svn.revision}</value>
</replacement>
<replacement>
<token>@buildtime@</token>
<value>${maven.build.timestamp}</value>
</replacement>
<replacement>
<token>@pomversion@</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
Version.java.template
package org.apache.iotdb.db.conf;
public final class Version {
public static final String build_number="@buildnumber@";
public static final String build_time="@buildtime@";
public static final String pomversion="@pomversion@";
}
這樣可以通過插件的方式,自動生成 version信息,解決版本信息獲取問題
package org.apache.xxx.db.conf;
public final class Version {
public static final String build_number="";
public static final String build_time="2021-02-04T07:59:49Z";
public static final String pomversion="0.12.0-SNAPSHOT";
}
另外提供 seata 項目 pom.xml 修改版本腳本 changeVersion.sh
echo "./changeVersion.sh oldVersion newVersion"
echo $1
echo $2
find ./ -name pom.xml | grep -v target | xargs perl -pi -e "s|$1|$2|g"
#find ./ -name Version.java | grep -v target | xargs perl -pi -e "s|$1|$2|g"
參考 https://github.com/seata/seata/blob/develop/changeVersion.sh