在實際開發過程中,開發環境,測試環境和最後部署上線的環境都是不一樣的,像資料庫連接配接,都是要變的。
如果不使用Maven的話,我想到的就是修改配置檔案,手動的修改;
使用Maven的話,就簡單的多了。
先來看一個pom檔案:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.ygy</groupId>
<artifactId>maven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 屬性配置 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<profiles>
<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>
<profile>
<id>test</id>
<properties>
<jdbc.url>http://www.deppon.com</jdbc.url>
<jdbc.username>haha</jdbc.username>
<jdbc.password>can you</jdbc.password>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- 添加Spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>maven</finalName>
</build>
</project>
其中,有些标簽可能沒有用過,就是<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://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="simple" class="org.ygy.maven.SimpleEntity">
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
</beans>
pom.xml就是最上面提到的:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.ygy</groupId>
<artifactId>maven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 屬性配置 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<profiles>
<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>
<profile>
<id>test</id>
<properties>
<jdbc.url>http://www.deppon.com</jdbc.url>
<jdbc.username>haha</jdbc.username>
<jdbc.password>can you</jdbc.password>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- 添加Spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>maven</finalName>
</build>
</project>
這裡有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.使用預設激活方式
mvn clean compile
進入target/classes目錄
打開demo.properties和applicationContext.xml檔案
會發現,在development中指定的屬性都已經成功替換
而demo.properties中,jdbc相關的并沒有配置,是以沒有替換
2.使用指令更改激活方式
重新輸入指令
mvn clean compile -P test
我們啟用了test環境的配置方式
再次進入target/classes檔案夾下檢視,會發現不同的替換
好了,到這裡就可以簡單使用了。