日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Apache Maven 使用 profile 和 filtering 实现多种环境下的资源配置管理

發布時間:2024/4/17 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Apache Maven 使用 profile 和 filtering 实现多种环境下的资源配置管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


http://archboy.org/2012/05/21/apache-maven-profile-filtering-multiple-build-environments/

構建項目時可能會遇到在測試(如單元測試)、開發、模擬、生產等不同環境下需要不同配置(properties、xml)或資源(jpg、png、mp3)的情況。比如常見的數據庫連接(即?jdbc url)的值,在不同的環境下可能有如下幾種值:

  • 測試環境:jdbc:mysql://localhost:3306/foobar_test
  • 開發環境:jdbc:mysql://localhost:3306/foobar_dev
  • 模擬環境:jdbc:mysql://192.168.1.11:3306/foobar
  • 生產環境:jdbc:mysql://192.168.1.10:3306/foobar

或者同樣是生產環境,針對(產品)交付給A公司客戶的與交付給B公司客戶的需要不同配置或者資源,比如產品界面中的公司名稱、公司LOGO等。

又或者針對不同的操作系統(如 Windows,Linux)需要為某個配置設定不同的文件路徑。

可見,在不同的軟件開發生命周期階段、不同的最終客戶(用戶)環境、不同的運行平臺都有可能需要不同配置或資源的情況。假如各個環境下的差別很小的話,我們可以在項目編譯之后手工修改或者寫個 shell script 自動修改,但如果需要修改的項目很多而且復雜的話,則應該使用?Apache Maven?的 Profile 和 Filtering 功能來解決。(當然前提是你的項目必須是用 Maven 構建的啦,哈哈,還有測試階段所使用到的資源文件實際上 Maven 默認已經劃分出來,所以并不需要本文所說的方法)

Filtering 功能

Filtering 是?Maven Resources Plugin 的一個功能,它會使用系統屬性或者項目屬性的值替換資源文件(*.properties,*.xml)當中 ${…} 符號的值。比如你系統屬性有一項 “user.name=foobar”,那么資源文件當中的 ${user.name} 符號會在 Maven 編譯時自動被替換為 “foobar”。

舉個例子:

默認的項目資源文件位于 “src/main/resources” 目錄,在該目錄下創建一個文件 “test.properties”,里面寫上一行:

Hello ${user.name}

然后修改項目文件(pom.xml)啟用 filtering 功能,如:

<project>...<build>...<resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource>...</resources>...</build>... </project>

然后編譯項目:

$mvn clean compile -Duser.name=foobar

查看輸出文件?target/classes/test.properties 的內容,可見原先的 “Hello {user.name}” 已經變成 “Hello foobar”。

我們也可以把 filtering 用到的變量寫在項目屬性段里,比如:

<project>...<properties><user.name>foobar</user.name><user.email>foobar@some.com</user.email></properties>... </project>

如果屬性項比較多的話,最佳實踐是把他們抽離出來獨立一個屬性文件,比如在你項目目錄(即 pom.xml 文件所在的目錄)新建一個屬性文件 project.properties:

user.name=foobar user.email=foobar@some.com

然后在 build/filters/filter 里指明使用這個屬性文件作為 filtering 屬性值的來源:

<project>...<build>...<filters><filter>project.properties</filter></filters>...</build>... </project>

Profile 功能

Profile 的作用是允許你在項目文件(pom.xml)里定義若干個 profile 段,然后在編譯時選擇其中的一個用于覆蓋項目文件原先的定義。接著上一個例子,如果我們需要為開發環境和生產環境定義不同的 user.name 屬性值,則我們在項目目錄里創建兩個屬性文件:

profile-development.properties,內容

user.name=foobar

profile-production.properties,內容

user.name=tom

然后在項目文件(pom.xml)里增加 profile 段,如下:

<project>...<profiles><profile><id>development</id><activation><activeByDefault>true</activeByDefault></activation><build><filters><filter>profile-development.properties</filter></filters></build></profile><profile><id>production</id><build><filters><filter>profile-production.properties</filter></filters></build></profile></profiles> </project>

在編譯項目時,可以使用 -P 參數指定需要使用的 profile 的 id,比如下面命令將會使用 development profile:

$mvn clean compile -Pdevelopment

如果想使用?production profile 則執行如下命令:

$mvn clean compile -Pproduction

假如不指定 -P 參數的話,則會使用 activeByDefault=true 的一項(即 development)。

至此,通過 filtering 和 profile 功能實現了為開發環境和生產環境使用不同配置值的目的。當然 profile 還可以允許你添加更多的定義,比如為某一個 profile 添加不同的資源文件。在一些大中型項目里,不同的環境可能僅僅修改配置值并不足夠,可能還需要某個配置文件整個替換,那么就應該在 profiles/profile/build/resources 段里指定了。詳細的可以參閱附錄鏈接。

附錄:

  • Maven Resources plugin – Filtering
  • Maven: The Complete Reference – Build Profiles


================================

http://piotrnowicki.com/2012/10/filtered-resources-in-maven/

Piotr Nowicki

About

Filtered Resources in Maven

Maven has some really great features. One of them is filtered resources.

Basically, if you use Maven for some time, I’m sure you know what by default everything you put in src/main/resources will be added to the classpath in the resulting artifact (e.g. in the WEB-INF/classes for a *.war archive.) Putting resources in this location is a common way of adding Java properties file to your project.

Now what is interesting: you can use Maven variables in those resource files and they can be swapped into final value during the process-resource phase. You just need to tell Maven that it is not a “regular” resource and that it should be filtered.

As an example: assume you have a following properties file:

src/main/resources/application.properties

app.version = ${version}

And you add the following section to your pom.xml file:

pom.xml

<project><build>...<resources><resource><directory>src/main/resources</directory><includes><include>application.properties</include></includes><filtering>true</filtering></resource></resources></build> <project>

During the prepare-resources phase the application.properties file will be filtered by Maven. The ${...} placeholders will be filled with proper values and the resulting application.properties will be produced and placed somewhere in the target directory. Its content will be something like:

app.version = 0.1.1-SNAPSHOT

You can use variables available out-of-the-box in Maven or some additional ones coming e.g. from the Maven plugins or your own defined in the <properties> pom.xml element. It’s pretty neat if you combine it with Maven Build Number plugin which can add your revision or some custom made build number and save it to your runtime accessible property file.

As a side note, if you define the exact files / directories you want to be filtered and want to treat the rest of the files as regular ones (not filtered) you should instruct Maven to do so. It seems that if you define at least one <resource> element, the default values doesn’t apply anymore. Something like this should do the work:

<project><build>...<resources><resource><directory>src/main/resources</directory><includes><include>application.properties</include></includes><filtering>true</filtering></resource><resource><directory>src/main/resources</directory></resource></resources></build> <project>

You can see an example of how I used it with Build Number plugin here.


總結

以上是生活随笔為你收集整理的Apache Maven 使用 profile 和 filtering 实现多种环境下的资源配置管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 人av在线| 看片国产 | 天天操操操操操 | 亚洲一区二区三区视频在线 | 9·1·黄·色·视·频 | 午夜美女在线 | 美乳在线播放 | 久久人体 | 亚洲国产一二三 | 天堂精品一区二区三区 | 超碰资源总站 | 精品免费在线观看 | 一个人免费在线观看视频 | 懂色av蜜臀av粉嫩av分享吧最新章节 | 九九激情网 | 国产精品丝袜黑色高跟 | 亚洲人一区二区三区 | 美女在线观看视频 | 99久久久久无码国产精品 | 欧洲av无码放荡人妇网站 | 性色av免费观看 | 国产精品免费久久 | 精品视频在线一区 | 狠久久 | 牛牛影视一区二区 | 精品国内自产拍在线观看视频 | 亚洲成人免费影院 | 国产黄色www| 免费麻豆国产一区二区三区四区 | 精品三级视频 | 亚洲无码精品免费 | 日本黄色一区二区 | 在线超碰91 | 热久久av| 欧美黑丝少妇 | 黑人和白人做爰 | 国产精品成人无码免费 | 美女在线播放 | 免费不卡av | 精品人妻无码一区二区三区换脸 | 卡通动漫亚洲综合 | 一级黄色网 | 黄色三级免费观看 | 淫综合网 | 国产午夜精品久久久 | 国产又色又爽又黄又免费 | 久久久国产精品一区 | 美女视频黄a视频全免费观看 | 中文字幕1区2区 | 动漫玉足吸乳羞免费网站玉足 | jizz中国女人高潮 | 蜜桃无码一区二区三区 | 爱情岛成人 | 奇米影视一区二区三区 | 国产福利av | 欧美裸体视频 | 色综合影视 | 国产成人免费看一级大黄 | 成人欧美一区二区三区在线播放 | 国产一区二区三区18 | 综合五月激情 | 91网页入口 | 96视频在线 | 免费网站观看www在线观看 | 在线播放一区二区三区 | 久久精品视频免费看 | 国产污污视频在线观看 | 精品乱码一区二区三区 | 日本人三级 | 一本色道综合久久欧美日韩精品 | 无码人妻精品一区二区三区在线 | 亚洲在线精品视频 | 好吊妞在线观看 | 日韩av一卡 | 小妹色播影院 | 亚洲专区在线播放 | 亚洲狼人色| 国产无遮挡a片又黄又爽 | 都市激情中文字幕 | 日本www色| 美丽的姑娘在线观看 | 波多野结衣1区2区3区 | 一区二区三区www污污污网站 | 欧美少妇15p| 99精品视频在线播放免费 | 能在线看的av | 日韩av无码一区二区三区 | 91一区二区视频 | 国产www色 | 国产精品桃色 | www.jizzcom| 欧美成人aaaaⅴ片在线看 | 国产奶头好大揉着好爽视频 | 亚洲第一伊人 | 亚洲国产一区二区三区四区 | 国产一级18片视频 | 亚洲国产成人一区二区 | 中文字幕免费在线看线人动作大片 | 国产一区二区三区小说 |