在實際開發過程中,開發環境,測試環境和最后部署上線的環境都是不一樣的,像數據庫連接,都是要變的。
如果不使用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的幾種方式](http://juvenshun.iteye.com/blog/208714))
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 實現多種環境下的資源](http://archboy.org/2012/05/21/apache-maven-profile-filtering-multiple-build-environments/))
Maven官方Filter講解:[http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html](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文件夾下查看,會發現不同的替換


好了,到這里就可以簡單使用了。
- 前言
- (一)- 環境搭建
- (二)- 安裝m2eclipse插件
- (三)- 使用Maven構建Web項目
- (四)- 使用Maven構建Web項目-測試
- (五)- 使用Maven構建Struts2項目
- (六)- 構建Hibernate項目
- (七)- 構建Spring項目
- (八)- 構建MyBatis項目
- (九)- 構建SSH項目
- (十) - 階段小結
- Maven深入學習(一)- 坐標
- Maven深入學習(二)- 依賴
- Maven深入學習(三)- 聚合與繼承
- Maven深入學習(四)- 知識總結
- Maven創建的Web項目無法使用EL表達式
- Maven知識點記錄 - profile
- Maven知識點記錄 - repositories
- Maven最佳實踐:版本管理
- Ubuntu上安裝Maven3
- Maven常用命令-創建Java項目
- Maven常用命令-創建Web項目
- Maven中引入本地jar包
- Maven私服(一) - The nexus service was launched, but failed to start.
- Maven私服(二) - Nexus的安裝