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

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

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

前言

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

Maven 的 profile:

在maven的 pom.xml 文件中有一個(gè)配置項(xiàng)叫著profiles節(jié)點(diǎn),如下:

 <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é)點(diǎn)下可以填寫多個(gè)profile 其中profile主要包含了三個(gè)屬性id、propertiesactivation 。

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

實(shí)踐前期準(zhǔn)備

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

在這里插入圖片描述

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

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

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

在這里插入圖片描述

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

在這里插入圖片描述

實(shí)踐一:

實(shí)踐一主要采用profile + filter的方式實(shí)現(xiàn)內(nèi)容的注入。
該方式的思想是通過 filter下的文件編寫可變動(dòng)的配置項(xiàng),由filters標(biāo)簽引入不同的配置文件項(xiàng),然后提取不同的配置文件中的配置項(xiàng)填充到resources下的配置文件中。
所以其中的關(guān)鍵標(biāo)簽包含了 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)容進(jìn)行注入-->
    <filtering>true</filtering>
    <directory>src/main/resources</directory>
    <includes>
     <include>**/*.properties</include>
     <include>**/*.xml</include>
    </includes>
   </resource>
  </resources>
 </build>
</project>

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

mvn clean install 

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

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

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

mvn clean package -Ptest # 指定激活項(xiàng)的ID

實(shí)踐二:

實(shí)踐二主要采用profile 直接將屬性配置到了profile下的properties節(jié)點(diǎn)下。此方法就不在需要多余的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中多了一個(gè)jdbc.url屬性。那我們的application.properties文件中同樣適用兩個(gè)占位符

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

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

mvn clean install -Pproduct

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

在這里插入圖片描述

結(jié)束語

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

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

相關(guān)文章

最新評論

屯留县| 稷山县| 甘德县| 仁寿县| 乐亭县| 仪陇县| 雅江县| 宜良县| 桂东县| 雅安市| 云梦县| 秦皇岛市| 乳源| 鸡东县| 关岭| 德格县| 扎兰屯市| 韩城市| 德江县| 新绛县| 永平县| 炎陵县| 固镇县| 江口县| 瑞金市| 九寨沟县| 镇宁| 双流县| 西宁市| 太和县| 唐山市| 余干县| 凭祥市| 灵寿县| 衡阳县| 阿克| 姚安县| 澄城县| 贺兰县| 华阴市| 贡觉县|