玩转Mavne-环境隔离

版权声明:转载需声明本人出品 https://blog.csdn.net/weixin_40288381/article/details/88907230

概述

实际企业级开发中,一个项目在本地开发完成后,需要提交到测试环境进行测试,测试完成后最终放到生产环境中运行。此时为了避免每次更换环境就要重新配置文件的繁琐操作,采用Maven的环境隔离实现简单的多环境配置

配置及原理

在pom.xml中的build节点里增加如下内容

<!-- Maven环境隔离-->
<!-- 配置源-->
<resources>
  <resource>
    <directory>src/main/resources.${deploy.type}</directory>
    <excludes>
      <exclude>*.jsp</exclude>
    </excludes>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
  </resource>
</resources>

在pom.xml中增加profiles节点,如下:

<profiles>
    <profile>
      <id>dev</id>
      <activation>
        <!-- 默认为Dev环境-->
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <deploy.type>dev</deploy.type>
      </properties>
    </profile>
    <profile>
      <id>beta</id>
      <properties>
        <deploy.type>beta</deploy.type>
      </properties>
    </profile>
    <profile>
      <id>prod</id>
      <properties>
        <deploy.type>prod</deploy.type>
      </properties>
    </profile>
  </profiles>

新建与环境对应的文件夹,并把要隔离的文件分开,公共的则留下

在这里插入图片描述

IDEA中设置默认环境:
在这里插入图片描述
因为我们在IDEA运行Tomcat的时候会发布war包,而这个打包过程使用什么环境,就是在这里配置的,所以我们才要在IDEA中设置一个默认的环境

编译打包命令:

mvn clean package -Dmaven.test.skip=true -Pdev # 开发环境的打包命令 跳过单元测试
mvn clean package -Dmaven.test.skip=true -Pbeta # 测试环境的打包命令 跳过单元测试
mvn clean package -Dmaven.test.skip=true -Pprod # 生产环境的打包命令 跳过单元测试

猜你喜欢

转载自blog.csdn.net/weixin_40288381/article/details/88907230