<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "利用filter讓Maven支援多重環境"]]></title>
		<link>https://forum.andowson.com/posts/list/6.page</link>
		<description><![CDATA[Latest messages posted in the topic "利用filter讓Maven支援多重環境"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>利用filter讓Maven支援多重環境</title>
				<description><![CDATA[ 最近在研究Maven 3，針對公司的實際環境需求，尤其是有關資料庫JDBC連線的設定，需要將測試環境及上線環境作個區隔，我找到了篇不錯的文章 
<br>
[url=http://sujitpal.blogspot.com/2006/10/maven2-multi-environment-filter-setup.html]A Maven2 multi-environment filter setup[/url] 
<br>
概念大致如下： 
<br>
在pom.xml中針對filter的檔名加上一個${env}的環境變數，如： 
<br>
[code]&lt;filters&gt; 
<br>
 &lt;filter&gt;src/main/filters/filter-${env}.properties&lt;/filter&gt; 
<br>
&lt;/filters&gt; 
<br>
&lt;resources&gt; 
<br>
 &lt;resource&gt; 
<br>
 &lt;directory&gt;src/main/resources&lt;/directory&gt; 
<br>
 &lt;filtering&gt;true&lt;/filtering&gt; 
<br>
 &lt;/resource&gt; 
<br>
&lt;/resources&gt; 
<br>
[/code] 
<br>
<br>
假設在src/main/resources/底下有個jdbc.properties，內容中有個變數是${db.host} 
<br>
<br>
我們只要在src/main/filters/底下編輯三個檔案，將跟資料庫相關的設定寫在裡面 
<br>
filter-dev.properties 
<br>
db.host=127.0.0.1 
<br>
<br>
filter-test.properties 
<br>
db.host=192.168.1.33 
<br>
<br>
filter-prod.properties 
<br>
db.host=192.168.2.99 
<br>
<br>
然後透過pom.xml中的profile設定 
<br>
[code]&lt;!-- default environment --&gt; 
<br>
&lt;properties&gt; 
<br>
 &lt;env&gt;dev&lt;/env&gt; 
<br>
&lt;/properties&gt; 
<br>
&lt;!-- profiles --&gt; 
<br>
&lt;profiles&gt; 
<br>
 &lt;profile&gt; 
<br>
 &lt;id&gt;dev&lt;/id&gt; 
<br>
 &lt;properties&gt; 
<br>
 &lt;env&gt;dev&lt;/env&gt; 
<br>
 &lt;/properties&gt; 
<br>
 &lt;/profile&gt; 
<br>
 &lt;profile&gt; 
<br>
 &lt;id&gt;test&lt;/id&gt; 
<br>
 &lt;properties&gt; 
<br>
 &lt;env&gt;test&lt;/env&gt; 
<br>
 &lt;/properties&gt; 
<br>
 &lt;/profile&gt; 
<br>
 &lt;profile&gt; 
<br>
 &lt;id&gt;prod&lt;/id&gt; 
<br>
 &lt;properties&gt; 
<br>
 &lt;env&gt;prod&lt;/env&gt; 
<br>
 &lt;/properties&gt; 
<br>
 &lt;/profile&gt; 
<br>
&lt;/profiles&gt; 
<br>
[/code] 
<br>
<br>
日後執行mvn指令時只要加上-P參數來指定要用那個profile即可，如 
<br>
mvn resources:resources &lt;=產生開發環境用的jdbc.properties 
<br>
mvn resources:resources -P test &lt;=產生測試環境用的jdbc.properties 
<br>
mvn resources:resources -P prod &lt;=產生上線環境用的jdbc.properties]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/458/930.page</guid>
				<link>https://forum.andowson.com/posts/preList/458/930.page</link>
				<pubDate><![CDATA[Thu, 9 Dec 2010 12:11:08]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>