【POM】maven profile切換正式環境和測試環境


有時候,我們在開發和部署的時候,有很多配置文件數據是不一樣的,比如連接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節點下新增規則

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

 

轉自:https://www.cnblogs.com/nfcm/p/7550772.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM