java運(yùn)行中獲取類(lèi)的包版本
方法
java運(yùn)行中獲取類(lèi)的包版本 AlphabetConverter.class 換成需要獲取包版本的包內(nèi)的任一類(lèi)
String fileFullPath = AlphabetConverter.class.getProtectionDomain().getCodeSource().getLocation().getFile();
//本地運(yùn)行潘明,獲取的內(nèi)容如下 /Users/cn/.m2/repository/org/apache/commons/commons-text/1.6/commons-text-1.6.jar
if(fileFullPath == null || fileFullPath.length() == 0){
throw new RuntimeException("getProtectionDomain().getCodeSource().getLocation().getFile() get null!");
}
String jarVersion = fileFullPath.substring(fileFullPath.lastIndexOf("/")).replace("包artifactId","").replace(".jar","");
用處
給使用方提供了一個(gè)功能jar包始藕,該jar包會(huì)定時(shí)從服務(wù)端獲取數(shù)據(jù),隨著jar包升級(jí)臼节,使用方增多接剩,會(huì)使得服務(wù)方對(duì)于調(diào)用方j(luò)ar包版本切厘,jar功能把控降低。
于是需要一個(gè)jar包通知機(jī)制/上報(bào)機(jī)制:jar通過(guò)上報(bào)接口定時(shí)通知服務(wù)端目前使用方名稱懊缺、jar包版本等信息
通過(guò)此功能可自動(dòng)識(shí)別jar版本