在實際開發過程中,開發環境,測試環境和最後部署上線的環境都是不一樣的,像數據庫連接,都是要變的。
如果不使用Maven的話,我想到的就是修改配置文件,手動的修改;
使用Maven的話,就簡單的多了。
先來看一個pom文件:
其中,有些標籤可能沒有用過,就是<profiles>,<profile>
Profile 的作用是允許你在項目文件(pom.xml)裏定義若干個 profile 段,然後在編譯時選擇其中的一個用於覆蓋項目文件原先的定義。
-
<profile>
-
<id>devlopment</id>
-
-
<properties>
-
<username>lufei</username>
-
<password>shishi</password>
-
</properties>
-
-
<build>
-
<resources>
-
<resource>
-
<directory>src/main/resources</directory>
-
<filtering>true</filtering>
-
</resource>
-
</resources>
-
</build>
-
-
<activation>
-
<activeByDefault>true</activeByDefault>
-
</activation>
-
-
</profile>
我們大體上可以看懂,下面簡單介紹一下具體的用法:
1.activation 激活方式
1)根據環境自動激活:如可以根據JDK版本,OS,Maven屬性來激活
-
<profile>
-
<id>dev</id>
-
<activation>
-
<activeByDefault>false</activeByDefault>
-
<jdk>1.5</jdk>
-
<os>
-
<name>Windows XP</name>
-
<family>Windows</family>
-
<arch>x86</arch>
-
<version>5.1.2600</version>
-
</os>
-
<property>
-
<name>mavenVersion</name>
-
<value>2.0.5</value>
-
</property>
-
<file>
-
<exists>file2.properties</exists>
-
<missing>file1.properties</missing>
-
</file>
-
</activation>
-
...
-
</profile>
2)通過命令行激活
這是最直接和最簡單的方式,比如你定義了一個名爲 myProfile 的 profile,你只需要在命令行輸入 mvn clean install -P myprofile 就能將其激活,
這種方式的好處很明顯,但是有一個很大的弊端,當 profile 比較多的時候,在命令行輸入這寫 -P 參數會讓人覺得厭煩,
所以,如果你一直用這種方式,覺得厭煩了,可以考慮使用其它自動激活的方式。
3)配置默認自動激活
-
<profile>
-
<id>dev</id>
-
<activation>
-
<activeByDefault>true</activeByDefault>
-
</activation>
-
...
-
</profile>
4)配置 settings.xml 文件 profile 激活
settings.xml 文件可以在 ~/.m2 目錄下,爲某個用戶的自定義行爲服務,也可以在 M2_HOME/conf 目錄下,爲整臺機器的所有用戶服務。
而前者的配置會覆蓋後者。同理,由 settings.xml 激活的 profile 意在爲用戶或者整個機器提供特定環境配置,
比如,你可以在某臺機器上配置一個指向本地數據庫 URL 的 profile,然後使用該機器的 settings.xml 激活它。激活方式如下:
-
<settings>
-
...
-
<activeProfiles>
-
<activeProfile>local_db</activeProfile>
-
</activeProfiles>
-
</settings>
(注:參考博客 激活Maven
profile的幾種方式)
2.filtering功能
這裏的意思是,過濾src/main/resources下的文件
-
<build>
-
<resources>
-
<resource>
-
<directory>src/main/resources</directory>
-
<filtering>true</filtering>
-
</resource>
-
</resources>
-
</build>
Filtering 是 Maven Resources Plugin 的一個功能,它會使用系統屬性或者項目屬性的值替換資源文件(*.properties,*.xml)當中 ${…} 符號的值。
比如你係統屬性有一項 “user.name=foobar”,那麼資源文件當中的 ${user.name} 符號會在 Maven 編譯時自動被替換爲 “foobar”。
(注:參考博客 Apache Maven 使用 profile 和 filtering 實現多種環境下的資源)
Maven官方Filter講解:http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
3.說了這麼多,下面來實踐一下
這是我們的Maven項目:
一個是配置文件,一個是Spring的配置文件
demo.properties
-
hello ,${username}
-
jdbc.url = ${jdbc.url}
-
jdbc.username = ${jdbc.username}
-
jdbc.password = ${jdbc.password}
applicationContext.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
-
xsi:schemaLocation="
-
http:
-
http:
-
http:
-
http:
-
http:
-
http:
-
http:
-
http:
-
-
<bean id="simple" class="org.ygy.maven.SimpleEntity">
-
<property name="username" value="${username}"></property>
-
<property name="password" value="${password}"></property>
-
</bean>
-
</beans>
pom.xml就是最上面提到的:
這裏有2個profile,一個是development,一個是test,默認自動激活development
注意
-
<properties>
-
<username>lufei</username>
-
<password>shishi</password>
-
</properties>
-
<properties>
-
<username>索隆</username>
-
<password>gogo</password>
-
<jdbc.url>http://www.deppon.com</jdbc.url>
-
<jdbc.username>haha</jdbc.username>
-
<jdbc.password>can you</jdbc.password>
-
</properties>
這裏的<username>和<password>就是我們在配置文件中使用的會變化的配置,Maven會自動將 ${}替換成profile中配置的。
接下來,我們進入到該項目的根目錄下,執行Maven命令
1.使用默認激活方式
進入target/classes目錄
打開demo.properties和applicationContext.xml文件
會發現,在development中指定的屬性都已經成功替換
而demo.properties中,jdbc相關的並沒有配置,所以沒有替換
2.使用命令更改激活方式
重新輸入命令
-
mvn clean compile -P test
我們啓用了test環境的配置方式
再次進入target/classes文件夾下查看,會發現不同的替換
好了,到這裏就可以簡單使用了。