1.将开发环境、测试环境、生产环境的配置文件分开存放,如下图:

 

2.在Maven中配置不同的环境打包配置文件的路径,配置如下:

复制代码
<profiles>  
        <profile>  
            <!-- 开发环境 -->  
            <id>dev</id>  
            <properties>  
                <env>dev</env>
            </properties>  
            <activation>  
                <!-- 默认激活该profile节点-->
                <activeByDefault>true</activeByDefault>  
            </activation> 
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources-env/dev</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>  
        <profile>  
            <!-- 测试环境 -->  
            <id>qa</id>  
            <properties>  
                <env>qa</env>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources-env/qa</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>    
        <profile>  
            <!-- 生产环境 -->  
            <id>online</id>  
            <properties>  
                <env>online</env>
            </properties>  
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources-env/online</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile> 
    </profiles>  
复制代码

 

3.项目打包前的配置

  右击项目->Maven->填入要打包的环境:

  

4.项目打包,当项目打包完以后解压后就可以看到在配置文件根目录中,已经把要打包的环境的配置文件都放进去了!

 

 转自:https://www.cnblogs.com/raphael5200/p/6677549.html