Maven知識點記錄 - profile

在實際開發過程當中,開發環境,測試環境和最後部署上線的環境都是不同的,像數據庫鏈接,都是要變的。 若是不使用Maven的話,我想到的就是修改配置文件,手動的修改; 使用Maven的話,就簡單的多了。 先來看一個pom文件: [html] view plaincopy <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>html

<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> java

其中,有些標籤可能沒有用過,就是<profiles>,<profile> Profile 的做用是容許你在項目文件(pom.xml)裏定義若干個 profile 段,而後在編譯時選擇其中的一個用於覆蓋項目文件原先的定義。 [html] view plaincopy <profile>
<id>devlopment</id>spring

<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屬性來激活 [html] view plaincopy <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)配置默認自動激活 [html] view plaincopy <profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
...
</profile>apache

4)配置 settings.xml 文件 profile 激活 settings.xml 文件能夠在 ~/.m2 目錄下,爲某個用戶的自定義行爲服務,也能夠在 M2_HOME/conf 目錄下,爲整臺機器的全部用戶服務。 而前者的配置會覆蓋後者。同理,由 settings.xml 激活的 profile 意在爲用戶或者整個機器提供特定環境配置, 好比,你能夠在某臺機器上配置一個指向本地數據庫 URL 的 profile,而後使用該機器的 settings.xml 激活它。激活方式以下: [html] view plaincopy <settings>
...
<activeProfiles>
<activeProfile>local_db</activeProfile>
</activeProfiles>
</settings>app

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

2.filtering功能 這裏的意思是,過濾src/main/resources下的文件 [html] view plaincopy <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.htmlui

3.說了這麼多,下面來實踐一下 這是咱們的Maven項目:url

一個是配置文件,一個是Spring的配置文件 demo.properties [java] view plaincopy hello ,${username}
jdbc.url = ${jdbc.url}
jdbc.username = ${jdbc.username}
jdbc.password = ${jdbc.password}

applicationContext.xml [java] view plaincopy

<?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就是最上面提到的: [html] view plaincopy <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 注意 [java] view plaincopy <properties>
<username>lufei</username>
<password>shishi</password>
</properties>

[html] view plaincopy <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.使用默認激活方式 [java] view plaincopy mvn clean compile

進入target/classes目錄

打開demo.properties和applicationContext.xml文件 會發現,在development中指定的屬性都已經成功替換

而demo.properties中,jdbc相關的並無配置,因此沒有替換

2.使用命令更改激活方式 從新輸入命令 [java] view plaincopy mvn clean compile -P test

咱們啓用了test環境的配置方式 再次進入target/classes文件夾下查看,會發現不一樣的替換

好了,到這裏就能夠簡單使用了。

相關文章
相關標籤/搜索