maven 替換指定檔案屬性值

2021-09-10 19:52:57 字數 1052 閱讀 1345

接到乙個需求:組裝 http 協議的時候在 header 裡面帶上 user-agent , 但是 agent 裡面需要動態獲取當前作業系統、系統架構、當前專案版本和名稱。本著最少維護成本的想法,否定了手動維護靜態變數的選擇,所以只能選擇通過現有的技術自動化解決問題。

在專案的 resources 目錄下建立 xx.properties 檔案(也可以在自己喜歡的目錄建立,不過稍後讀取的時候會比較麻煩,所以建議是在 resources 下面建立),填寫以下資訊

project.version=$

project.name=$

看名字就知道他們是什麼意思了就不解釋了

在 pom 檔案的 build 標籤裡填入相應資訊

src/main/resources

xx.properties

true

directory:指定需要替換的檔案目錄

include:指定需要替換的檔案(可以使用*.*來通配)

filtering:true 開啟替換

最後就是在**裡面讀取配置

properties props = system.getproperties();

// 這裡有個小問題 getsystemresourceasstream 和 getresourceasstream 是有區別的,前者如果打成 jar 被依賴的話會報xx.properties找不到

// 替**法,場景不同可以選需要的寫法

// props.load(xx.class.getclassloader().getresourceasstream("xx.properties"));

props.load(classloader.getsystemresourceasstream("xx.properties"));

string name = props.getproperty("project.name");

string version = props.getproperty("project.version")

SpringBoot 讀取配置檔案屬性值

springboot有兩種配置檔案properties和yml配置檔案,但讀取的方式都是一樣的。主要有以下的兩種方式,以yml檔案作為講解 yml檔案 auth authurl cookiename auth.session.id version 1.01 使用 value註解將配置檔案的屬性注入到...

springboot 獲取配置檔案屬性值

兩種方式 configurationproperties prefix person 這個註解指定配置檔案中的person物件屬性 component configurationproperties prefix person propertysource value classpath perso...

設定檔案屬性

程式設計時經常需要某些檔案擁有特定的屬性,譬如隱藏屬性。下面介紹乙個實現此功能的函式 setfileattributes。函式原型 函式原型 boolsetfileattributes lpctstr lpfilename,lpfilename的物件指向乙個字串,指定其屬性要設定的檔案的名稱。例如 ...