maven 參數化構建時 修改配置文件的值

maven構建出現版本號

mvn clean package -Dmaven.test.skip=true -P prod -DprofileServerVersion=$tag_version
參數講解

跳過測試,這裏的跳過測試,通常開發中junit單元測試
-Dmaven.test.skip=true
選擇maven啓動的類型,生產還是開發環境
-P prod
選擇maven啓動的版本號,這裏的tag_version是jenkins參數化構建自定義的一個參數
-DprofileServerVersion=$tag_version
在這裏插入圖片描述
-DprofileServerVersion=$tag_version
這裏的意思是在構建過程中,可以修改項目的配置文件。我下面的示例將會修改yml文件,同理修改properties文件與修改yml中是一致的。

pom的build中添加掃描路徑

在pom的build中,添加需要修改文件的路徑。<include>application.yml</include> 這裏寫死了application.yml文件路徑,當然你也可以寫成相對路徑。

<build>
 <resources>
  <resource>
      <directory>src/main/resources</directory>
      <includes>
          <include>**/*</include>
      </includes>
  </resource>
  <resource>
      <directory>src/main/resources</directory>
      <includes>
          <include>application.yml</include>
      </includes>
      <filtering>true</filtering>
  </resource>
</resources>
</build>
在pom的profile接收構建參數

pom文件中,添加profile,這樣在命令構建的過程中就可以指定參數。

<profiles>
 <profile>
     <id>prod</id>
     <properties>
         <profileActive>prod</profileActive>
         <!--這裏的profileServerVersion就是,
         maven構建的-DprofileServerVersion參數,
         前面的-D後面的內容 -->
         <profileServerVersion>release_1.0.0</profileServerVersion>
     </properties>
 </profile>
</profiles>
yml文件中添加接收參數的參數和佔位符

在application.yml添加此內容即可 server.version: @profileServerVersion@ 。值得注意 @profileServerVersion@ 就是接收 -DprofileServerVersion=$tag_version 的地方。

總結:

三步出發
第一步在pom的build中添加掃描路徑,
第二步在pom的profile接收構建參數,
第三部在yml文件中添加接收參數的參數和佔位符。

爲了便於您的查看,該博客的源碼地址在:https://gitee.com/cnhellorui/some_source_code/tree/master/serviceApiVersion

如果存在錯誤和建議,請給我發郵件或者留言 [email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章