最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Maven profile實現(xiàn)不同環(huán)境的配置管理實踐

 更新時間:2020年09月19日 09:35:54   作者:丑過三八線  
這篇文章主要介紹了Maven profile實現(xiàn)不同環(huán)境的配置管理實踐,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言

目前,企業(yè)項目的開發(fā)過程中,往往會使用配置文件來做一些配置項來實現(xiàn)項目部署的靈活性,避免硬編碼的方式在環(huán)境變化時需要對代碼進行重新編譯。但是往往在項目周期中存在著各種環(huán)境:如開發(fā)環(huán)境、測試環(huán)境以及生產(chǎn)環(huán)境等,而且在不同的運行環(huán)境中可能牽扯到大量的配置項變化。如果在不同的部署環(huán)境中切換,對配置文件的管理往往容易讓程序員感覺非常的混亂。
為了避免這種換亂,研發(fā)過程中也有比較多的手段進行。比如,有公司就采用VPN的虛擬網(wǎng)絡環(huán)境,讓測試環(huán)境、生產(chǎn)環(huán)境的網(wǎng)絡一致,讓程序員在不同環(huán)境中對版本進行發(fā)布時只需要對VPN進行切換即可。以免發(fā)生網(wǎng)絡配置項改錯,漏改等現(xiàn)象的發(fā)生。這樣個人覺得還不錯,唯一有一點句是調(diào)整網(wǎng)絡環(huán)境、設(shè)備環(huán)境的成本應該也比較高。
當然profile的方式應該算是比較經(jīng)濟的。我知道的比如spring-boot、maven都可以支持到profile的方式來對不同環(huán)境進行指定。本文希望介紹一下,我理解的使用maven的profile方式來進行不同環(huán)境切換。講得不到位的地方希望看官嘴下留情,也多指定。

Maven 的 profile:

在maven的 pom.xml 文件中有一個配置項叫著profiles節(jié)點,如下:

 <profiles>
  <profile>
   <id>test</id>
   <properties>
    <active.profile>test</active.profile>
    <jdbc.url>127.0.0.1</jdbc.url>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>
  <profile>
   <id>develop</id>
   <properties>
    <active.profile>develop</active.profile>
    <jdbc.url>192.168.1.102</jdbc.url>
   </properties>
   <activation>
    <activeByDefault>true</activeByDefault>
   </activation>
  </profile>
  <profile>
   <id>product</id>
   <properties>
    <actived.profile>product</actived.profile>
    <jdbc.url>10.21.41.100</jdbc.url>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>
 </profiles>

其中profiles節(jié)點下可以填寫多個profile 其中profile主要包含了三個屬性id、propertiesactivation 。

id 應該是為了區(qū)分profile用的。
properties 就是對應的屬性。
activation 應該是主要用來指定是否被默認激活的,它還有一個子節(jié)點activeByDefault, 如果子節(jié)點activeByDefault內(nèi)的值為true表示他會被激活。它還有一些子節(jié)點,但是不知道什么用。后續(xù)看看在學習下。

實踐前期準備

我準備建立一個簡單的maven功能來實踐一下maven的profile實現(xiàn)不同的配置管理,所以首先需要建議一個maven工程。本文采用的是idea進行試驗的。一下是我建立工程的結(jié)構(gòu)圖。

在這里插入圖片描述

由于我只是需要對配置文件進行管理,所以是完全不需要建立任何java類就可以的。

  • 首先建立了三個profile相關(guān)的配置文件 develop.properties 、 product.propertiestest.properties
  • 在三個文件中我就只寫了一個屬性app.name,三個文件中分別為 develop.maven.profile、 product.maven.profiletest.maven.profile.
  • 為了驗證在各類配置文件中都是可行的, 我建立了兩個供測試的配置文件application.properties、 application.xml.

application.properties 的內(nèi)容為:

在這里插入圖片描述

application.xml的內(nèi)容為:

在這里插入圖片描述

實踐一:

實踐一主要采用profile + filter的方式實現(xiàn)內(nèi)容的注入。
該方式的思想是通過 filter下的文件編寫可變動的配置項,由filters標簽引入不同的配置文件項,然后提取不同的配置文件中的配置項填充到resources下的配置文件中。
所以其中的關(guān)鍵標簽包含了 profile filter resource

Maven的配置文件pom.xml的內(nèi)容如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent>
  <artifactId>maven-test</artifactId>
  <groupId>com.maomao.maven</groupId>
  <version>1.0-SNAPSHOT</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <artifactId>maven-profile</artifactId>
 <version>1.0-SNAPSHOT</version>
 <profiles>
  <profile>
   <id>test</id>
   <properties>
    <active.profile>test</active.profile>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>
  <profile>
   <id>develop</id>
   <properties>
    <active.profile>develop</active.profile>
   </properties>
   <activation>
    <activeByDefault>true</activeByDefault>
   </activation>
  </profile>
  <profile>
   <id>product</id>
   <properties>
    <actived.profile>product</actived.profile>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>
 </profiles>
 <build>
  <filters>
   <filter>src/filters/${active.profile}.properties</filter>
  </filters>
  <resources>
   <resource>
   <!--一定要讓filtering為true 否則無法對內(nèi)容進行注入-->
    <filtering>true</filtering>
    <directory>src/main/resources</directory>
    <includes>
     <include>**/*.properties</include>
     <include>**/*.xml</include>
    </includes>
   </resource>
  </resources>
 </build>
</project>

執(zhí)行maven命令進行打包:

mvn clean install 

執(zhí)行后打包結(jié)果targets文件夾下的配置文件application.properties、 application.xml的占位置被填充。

在這里插入圖片描述
在這里插入圖片描述

當然如果切換profiles下的激活項,填充的內(nèi)容自然也會發(fā)生變化?;蛘卟捎胢aven命令進行激活項的切換:

mvn clean package -Ptest # 指定激活項的ID

實踐二:

實踐二主要采用profile 直接將屬性配置到了profile下的properties節(jié)點下。此方法就不在需要多余的filter properties文件配置了。
例如:

 <profile>
   <id>product</id>
   <properties>
    <actived.profile>product</actived.profile>
    <jdbc.url>10.21.41.100</jdbc.url>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>

這里properites中多了一個jdbc.url屬性。那我們的application.properties文件中同樣適用兩個占位符

app.name=${app.name}
jdbc.url=${jdbc.url}

同樣執(zhí)行maven命令進行打包:

mvn clean install -Pproduct

打包之后的 application.properties內(nèi)容對應變?yōu)?/p>

在這里插入圖片描述

結(jié)束語

整個內(nèi)容寫的有點亂,但是意思大概就是這個意思。主要想說maven可以搞這樣一個事情。也給自己留個備忘錄。如果有人來看到這個希望輕噴,我很少寫東西。最近準備練習寫東西,待改進的地方還是很多的,所以見諒了。

到此這篇關(guān)于Maven profile實現(xiàn)不同環(huán)境的配置管理實踐的文章就介紹到這了,更多相關(guān)Maven profile配置管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

石楼县| 皮山县| 靖宇县| 西充县| 班玛县| 合江县| 靖边县| 沂水县| 准格尔旗| 天长市| 耿马| 扶沟县| 周至县| 普安县| 密云县| 雷波县| 饶平县| 疏勒县| 扬州市| 江油市| 马公市| 阳新县| 五指山市| 南城县| 稻城县| 化德县| 庆安县| 襄城县| 芦溪县| 扶沟县| 潍坊市| 芜湖市| 海城市| 抚宁县| 马公市| 凤翔县| 鄂州市| 象山县| 前郭尔| 河池市| 逊克县|