Maven學習(十六)--Maven知識點記錄 - profile

在實際開發過程中,開發環境,測試環境和最後部署上線的環境都是不一樣的,像數據庫連接,都是要變的。

如果不使用Maven的話,我想到的就是修改配置文件,手動的修改;

使用Maven的話,就簡單的多了。

先來看一個pom文件:

[html] view plaincopy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>org.ygy</groupId>  
  6.     <artifactId>maven</artifactId>  
  7.     <packaging>war</packaging>  
  8.     <version>0.0.1-SNAPSHOT</version>  
  9.   
  10.     <name>maven Maven Webapp</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <!-- 屬性配置 -->  
  14.     <properties>  
  15.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  16.     </properties>  
  17.   
  18.   
  19.     <profiles>  
  20.         <profile>  
  21.             <id>devlopment</id>  
  22.   
  23.             <properties>  
  24.                 <username>lufei</username>  
  25.                 <password>shishi</password>  
  26.             </properties>  
  27.   
  28.             <build>  
  29.                 <resources>  
  30.                     <resource>  
  31.                         <directory>src/main/resources</directory>  
  32.                         <filtering>true</filtering>  
  33.                     </resource>  
  34.                 </resources>  
  35.             </build>  
  36.   
  37.             <activation>  
  38.                 <activeByDefault>true</activeByDefault>  
  39.             </activation>  
  40.   
  41.         </profile>  
  42.   
  43.         <profile>  
  44.             <id>test</id>  
  45.             <properties>  
  46.                 <jdbc.url>http://www.deppon.com</jdbc.url>  
  47.                 <jdbc.username>haha</jdbc.username>  
  48.                 <jdbc.password>can you</jdbc.password>  
  49.             </properties>  
  50.             <build>  
  51.                 <resources>  
  52.                     <resource>  
  53.                         <directory>src/main/resources</directory>  
  54.                         <filtering>true</filtering>  
  55.                     </resource>  
  56.                 </resources>  
  57.             </build>  
  58.             <activation>  
  59.                 <activeByDefault>false</activeByDefault>  
  60.             </activation>  
  61.         </profile>  
  62.     </profiles>  
  63.   
  64.     <dependencies>  
  65.         <dependency>  
  66.             <groupId>junit</groupId>  
  67.             <artifactId>junit</artifactId>  
  68.             <version>4.10</version>  
  69.             <scope>test</scope>  
  70.         </dependency>  
  71.   
  72.         <!-- 添加Spring依賴 -->  
  73.         <dependency>  
  74.             <groupId>org.springframework</groupId>  
  75.             <artifactId>spring-core</artifactId>  
  76.             <version>3.1.1.RELEASE</version>  
  77.         </dependency>  
  78.   
  79.         <dependency>  
  80.             <groupId>org.springframework</groupId>  
  81.             <artifactId>spring-beans</artifactId>  
  82.             <version>3.1.1.RELEASE</version>  
  83.         </dependency>  
  84.   
  85.         <dependency>  
  86.             <groupId>org.springframework</groupId>  
  87.             <artifactId>spring-context</artifactId>  
  88.             <version>3.1.1.RELEASE</version>  
  89.         </dependency>  
  90.   
  91.         <dependency>  
  92.             <groupId>org.springframework</groupId>  
  93.             <artifactId>spring-jdbc</artifactId>  
  94.             <version>3.1.1.RELEASE</version>  
  95.         </dependency>  
  96.   
  97.     </dependencies>  
  98.   
  99.     <build>  
  100.         <finalName>maven</finalName>  
  101.     </build>  
  102. </project>  

其中,有些標籤可能沒有用過,就是<profiles>,<profile>

Profile 的作用是允許你在項目文件(pom.xml)裏定義若干個 profile 段,然後在編譯時選擇其中的一個用於覆蓋項目文件原先的定義。

[html] view plaincopy
  1. <profile>  
  2.             <id>devlopment</id>  
  3.   
  4.             <properties>  
  5.                 <username>lufei</username>  
  6.                 <password>shishi</password>  
  7.             </properties>  
  8.   
  9.             <build>  
  10.                 <resources>  
  11.                     <resource>  
  12.                         <directory>src/main/resources</directory>  
  13.                         <filtering>true</filtering>  
  14.                     </resource>  
  15.                 </resources>  
  16.             </build>  
  17.   
  18.             <activation>  
  19.                 <activeByDefault>true</activeByDefault>  
  20.             </activation>  
  21.   
  22.         </profile>  

我們大體上可以看懂,下面簡單介紹一下具體的用法:

1.activation 激活方式

1)根據環境自動激活:如可以根據JDK版本,OS,Maven屬性來激活

[html] view plaincopy
  1. <profile>  
  2.   <id>dev</id>  
  3.   <activation>  
  4.     <activeByDefault>false</activeByDefault>  
  5.     <jdk>1.5</jdk>  
  6.     <os>  
  7.       <name>Windows XP</name>  
  8.       <family>Windows</family>  
  9.       <arch>x86</arch>  
  10.       <version>5.1.2600</version>  
  11.     </os>  
  12.     <property>  
  13.       <name>mavenVersion</name>  
  14.       <value>2.0.5</value>  
  15.     </property>  
  16.     <file>  
  17.       <exists>file2.properties</exists>  
  18.       <missing>file1.properties</missing>  
  19.     </file>  
  20.   </activation>  
  21.   ...  
  22. </profile>  

2)通過命令行激活

這是最直接和最簡單的方式,比如你定義了一個名爲 myProfile 的 profile,你只需要在命令行輸入 mvn clean install -P myprofile 就能將其激活,

這種方式的好處很明顯,但是有一個很大的弊端,當 profile 比較多的時候,在命令行輸入這寫 -P 參數會讓人覺得厭煩,

所以,如果你一直用這種方式,覺得厭煩了,可以考慮使用其它自動激活的方式。

3)配置默認自動激活

[html] view plaincopy
  1. <profile>  
  2.   <id>dev</id>  
  3.   <activation>  
  4.     <activeByDefault>true</activeByDefault>  
  5.   </activation>  
  6.   ...  
  7. </profile>  

4)配置 settings.xml 文件 profile 激活

settings.xml 文件可以在 ~/.m2 目錄下,爲某個用戶的自定義行爲服務,也可以在 M2_HOME/conf 目錄下,爲整臺機器的所有用戶服務。

而前者的配置會覆蓋後者。同理,由 settings.xml 激活的 profile 意在爲用戶或者整個機器提供特定環境配置,

比如,你可以在某臺機器上配置一個指向本地數據庫 URL 的 profile,然後使用該機器的 settings.xml 激活它。激活方式如下:

[html] view plaincopy
  1. <settings>  
  2.   ...  
  3.   <activeProfiles>  
  4.     <activeProfile>local_db</activeProfile>  
  5.   </activeProfiles>  
  6. </settings>  

(注:參考博客  激活Maven profile的幾種方式


2.filtering功能

這裏的意思是,過濾src/main/resources下的文件

[html] view plaincopy
  1. <build>  
  2.                 <resources>  
  3.                     <resource>  
  4.                         <directory>src/main/resources</directory>  
  5.                         <filtering>true</filtering>  
  6.                     </resource>  
  7.                 </resources>  
  8.             </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

[java] view plaincopy
  1. hello ,${username}  
  2. jdbc.url = ${jdbc.url}  
  3. jdbc.username = ${jdbc.username}  
  4. jdbc.password = ${jdbc.password}  

applicationContext.xml

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.      http://www.springframework.org/schema/beans   
  7.      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.      http://www.springframework.org/schema/tx   
  9.      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  10.      http://www.springframework.org/schema/aop   
  11.      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  12.      http://www.springframework.org/schema/context  
  13.      http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  14.   
  15.     <bean id="simple" class="org.ygy.maven.SimpleEntity">  
  16.         <property name="username" value="${username}"></property>  
  17.         <property name="password" value="${password}"></property>  
  18.     </bean>  
  19. </beans>  

pom.xml就是最上面提到的:

[html] view plaincopy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>org.ygy</groupId>  
  6.     <artifactId>maven</artifactId>  
  7.     <packaging>war</packaging>  
  8.     <version>0.0.1-SNAPSHOT</version>  
  9.   
  10.     <name>maven Maven Webapp</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <!-- 屬性配置 -->  
  14.     <properties>  
  15.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  16.     </properties>  
  17.   
  18.   
  19.     <profiles>  
  20.         <profile>  
  21.             <id>devlopment</id>  
  22.   
  23.             <properties>  
  24.                 <username>lufei</username>  
  25.                 <password>shishi</password>  
  26.             </properties>  
  27.   
  28.             <build>  
  29.                 <resources>  
  30.                     <resource>  
  31.                         <directory>src/main/resources</directory>  
  32.                         <filtering>true</filtering>  
  33.                     </resource>  
  34.                 </resources>  
  35.             </build>  
  36.   
  37.             <activation>  
  38.                 <activeByDefault>true</activeByDefault>  
  39.             </activation>  
  40.   
  41.         </profile>  
  42.   
  43.         <profile>  
  44.             <id>test</id>  
  45.             <properties>  
  46.                 <jdbc.url>http://www.deppon.com</jdbc.url>  
  47.                 <jdbc.username>haha</jdbc.username>  
  48.                 <jdbc.password>can you</jdbc.password>  
  49.             </properties>  
  50.             <build>  
  51.                 <resources>  
  52.                     <resource>  
  53.                         <directory>src/main/resources</directory>  
  54.                         <filtering>true</filtering>  
  55.                     </resource>  
  56.                 </resources>  
  57.             </build>  
  58.             <activation>  
  59.                 <activeByDefault>false</activeByDefault>  
  60.             </activation>  
  61.         </profile>  
  62.     </profiles>  
  63.   
  64.     <dependencies>  
  65.         <dependency>  
  66.             <groupId>junit</groupId>  
  67.             <artifactId>junit</artifactId>  
  68.             <version>4.10</version>  
  69.             <scope>test</scope>  
  70.         </dependency>  
  71.   
  72.         <!-- 添加Spring依賴 -->  
  73.         <dependency>  
  74.             <groupId>org.springframework</groupId>  
  75.             <artifactId>spring-core</artifactId>  
  76.             <version>3.1.1.RELEASE</version>  
  77.         </dependency>  
  78.   
  79.         <dependency>  
  80.             <groupId>org.springframework</groupId>  
  81.             <artifactId>spring-beans</artifactId>  
  82.             <version>3.1.1.RELEASE</version>  
  83.         </dependency>  
  84.   
  85.         <dependency>  
  86.             <groupId>org.springframework</groupId>  
  87.             <artifactId>spring-context</artifactId>  
  88.             <version>3.1.1.RELEASE</version>  
  89.         </dependency>  
  90.   
  91.         <dependency>  
  92.             <groupId>org.springframework</groupId>  
  93.             <artifactId>spring-jdbc</artifactId>  
  94.             <version>3.1.1.RELEASE</version>  
  95.         </dependency>  
  96.   
  97.     </dependencies>  
  98.   
  99.     <build>  
  100.         <finalName>maven</finalName>  
  101.     </build>  
  102. </project>  

這裏有2個profile,一個是development,一個是test,默認自動激活development

注意

[java] view plaincopy
  1. <properties>  
  2.                 <username>lufei</username>  
  3.                 <password>shishi</password>  
  4.             </properties>  


[html] view plaincopy
  1. <properties>  
  2.                 <username>索隆</username>  
  3.                 <password>gogo</password>  
  4.                 <jdbc.url>http://www.deppon.com</jdbc.url>  
  5.                 <jdbc.username>haha</jdbc.username>  
  6.                 <jdbc.password>can you</jdbc.password>  
  7.             </properties>  


這裏的<username>和<password>就是我們在配置文件中使用的會變化的配置,Maven會自動將 ${}替換成profile中配置的。

接下來,我們進入到該項目的根目錄下,執行Maven命令


1.使用默認激活方式

[java] view plaincopy
  1. mvn clean compile  


進入target/classes目錄

打開demo.properties和applicationContext.xml文件

會發現,在development中指定的屬性都已經成功替換


而demo.properties中,jdbc相關的並沒有配置,所以沒有替換



2.使用命令更改激活方式

重新輸入命令

[java] view plaincopy
  1. mvn clean compile -P test  

我們啓用了test環境的配置方式

再次進入target/classes文件夾下查看,會發現不同的替換


好了,到這裏就可以簡單使用了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章