1. 程式人生 > >maven profile切換正式環境和測試環境 maven profile切換正式環境和測試環境

maven profile切換正式環境和測試環境 maven profile切換正式環境和測試環境

maven profile切換正式環境和測試環境

 

討論QQ群:313032825

有時候,我們在開發和部署的時候,有很多配置檔案資料是不一樣的,比如連線mysql,連線redis,一些properties檔案等等

每次部署或者開發都要改配置檔案太麻煩了,這個時候,就需要用到maven的profile配置了

1,在專案下pom.xml的project節點下建立了開發環境和線上環境的profile

複製程式碼
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <env>prd</env>
            </properties>
        </profile>
    </profiles>
複製程式碼

其中id代表這個環境的唯一標識,下面會用到
properties下我們我們自己自定義了標籤env,內容分別是dev和prd。

activeByDefault=true代表如果不指定某個固定id的profile,那麼就使用這個環境

 

 

2,下面是我們resources下的目錄,有兩個目錄,dev和prd,在開發時,我們使用dev下的配置檔案,部署時候使用prd下的配置檔案

 

 

3配置pom.xml,如果直接install,那麼就會找到預設的id為dev的這個profile,然後會在裡面找env的節點的值,

接著就會執行替換,相當於將src/main/resources/dev這個資料夾下的所有的配置檔案打包到classes根目錄下。

複製程式碼
    <build>
        <finalName>springmvc2</finalName>
        <resources>
            <resource>
                <directory>src/main/resources/${env}</directory>
            </resource>
        </resources>
    </build>
複製程式碼

 在idea下指定打包時指定profile的id

1第一步

 2第二步

 

3第三步

4第四步,執行打包命令

 

這個時候target下springmvc專案下的classes根目錄下有了env.properties,並且裡面內容是我們指定的那個env.properties,

但是發現resources下的aa.properties檔案沒有被打包進去,那是因為我們只指定了resources/prd下的檔案打包到根目錄下,並沒有指定其他檔案

我們在pom.xml下的resources節點下新增規則

複製程式碼
<resource>
    <directory>src/main/resources</directory>
    <excludes>
        <exclude>dev/*</exclude>
        <exclude>prd/*</exclude>
    </excludes>
</resource>
複製程式碼

再執行打包就會將resources下的除了dev資料夾和prd資料夾的其他所有檔案打包到classes根目錄下了

 

 

最後注意:如果有其他配置檔案在src/main/java目錄下,也是可以這樣指定的,但是要指定

<includes>
    <include>*.xml</include>
</includes>

不然會將java類當配置檔案一塊放到classes根目錄下,加了include就會只匹配符合條件的放到target的classes根目錄下

 最後放我的所有的配置

複製程式碼
   <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <env>prd</env>
            </properties>
        </profile>
    </profiles>


    <build>
        <finalName>springmvc</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>prd/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${env}</directory>
            </resource>
        </resources>
    </build>
複製程式碼

 

作者: nfcm 出處: http://www.cnblogs.com/nfcm/ qq討論群:313032825 如果你覺得文章對你有用,不妨點下右下角的推薦按鈕。 本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。

討論QQ群:313032825

有時候,我們在開發和部署的時候,有很多配置檔案資料是不一樣的,比如連線mysql,連線redis,一些properties檔案等等

每次部署或者開發都要改配置檔案太麻煩了,這個時候,就需要用到maven的profile配置了

1,在專案下pom.xml的project節點下建立了開發環境和線上環境的profile

複製程式碼
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <env>prd</env>
            </properties>
        </profile>
    </profiles>
複製程式碼

其中id代表這個環境的唯一標識,下面會用到
properties下我們我們自己自定義了標籤env,內容分別是dev和prd。

activeByDefault=true代表如果不指定某個固定id的profile,那麼就使用這個環境

 

 

2,下面是我們resources下的目錄,有兩個目錄,dev和prd,在開發時,我們使用dev下的配置檔案,部署時候使用prd下的配置檔案

 

 

3配置pom.xml,如果直接install,那麼就會找到預設的id為dev的這個profile,然後會在裡面找env的節點的值,

接著就會執行替換,相當於將src/main/resources/dev這個資料夾下的所有的配置檔案打包到classes根目錄下。

複製程式碼
    <build>
        <finalName>springmvc2</finalName>
        <resources>
            <resource>
                <directory>src/main/resources/${env}</directory>
            </resource>
        </resources>
    </build>
複製程式碼

 在idea下指定打包時指定profile的id

1第一步

 2第二步

 

3第三步

4第四步,執行打包命令

 

這個時候target下springmvc專案下的classes根目錄下有了env.properties,並且裡面內容是我們指定的那個env.properties,

但是發現resources下的aa.properties檔案沒有被打包進去,那是因為我們只指定了resources/prd下的檔案打包到根目錄下,並沒有指定其他檔案

我們在pom.xml下的resources節點下新增規則

複製程式碼
<resource>
    <directory>src/main/resources</directory>
    <excludes>
        <exclude>dev/*</exclude>
        <exclude>prd/*</exclude>
    </excludes>
</resource>
複製程式碼

再執行打包就會將resources下的除了dev資料夾和prd資料夾的其他所有檔案打包到classes根目錄下了

 

 

最後注意:如果有其他配置檔案在src/main/java目錄下,也是可以這樣指定的,但是要指定

<includes>
    <include>*.xml</include>
</includes>

不然會將java類當配置檔案一塊放到classes根目錄下,加了include就會只匹配符合條件的放到target的classes根目錄下

 最後放我的所有的配置

複製程式碼
   <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <env>prd</env>
            </properties>
        </profile>
    </profiles>


    <build>
        <finalName>springmvc</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>prd/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${env}</directory>
            </resource>
        </resources>
    </build>
複製程式碼