接口自动化增加环境隔离功能

1. pom文件设置: 

在build节点下增加:

<resources>
      <resource>
        <!-- 指定配置文件所在的目录,${deploy.type}是一个参数变量,通过这个变量来决定加载哪个环境的配置文件 -->
        <directory>src/main/resources.${deploy.type}</directory>
        <!-- 排除.jsp文件,也就是不加载.jsp文件 -->
        <excludes>
          <exclude>*.jsp</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>

插入新节点:

<profiles>
    <!-- 测试环境 -->
    <profile>
      <id>uat</id>
      <activation>
        <!-- 表示如果编译时没有指定环境,则以该环境为默认环境 -->
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <deploy.type>uat</deploy.type>
      </properties>
    </profile>

    <!-- 生产环境 -->
    <profile>
      <id>prod</id>
      <properties>
        <deploy.type>prod</deploy.type>
      </properties>
    </profile>
  </profiles>

2.  数据源:在main目录下新建两个文件夹

接口自动化增加环境隔离功能

在两个文件夹下,分别创建一个名为 datasource.properties 的文件

接口自动化增加环境隔离功能

 3. 读取properties数据的util方法

private static Properties properties;

/*
     *从datasource中取数据
     */
    public String getValue(String key) throws IOException {
        String propertiesFileName = "datasource.properties";
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream(propertiesFileName);
        properties = new Properties();
        properties.load(stream);
        String value = properties.getProperty(key);
        return value;
    }