概念理解:

    • profile:profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。
    • resources:resources是指定maven编译资源文件指定到何处。filtering:开启过滤,用指定的参数替换directory下的文件中的参数(eg. ${name}),directory:指定资源文件的位置。
    • filters:可以理解为筛选器或者填充器,filters里面可以配置多个filter,但是会以最后一个为准,所以一般只配置一个filter。使用filter的参数填充resources中的directory下的文件中的${xxx}占位符

    主要的实现方法:
    利用resources和filter,实现对配置文件的动态赋值
    利用profile进行filters文件的切换控制

    实现步骤:
    1、创建目标配置文件,在目标文件中使用${xxx}进行属性引用
    env.properties

    1. #Environment
    2. environment=${environment}
    3. api.host=${api.host}

    2、在main/filters目录下创建filter文件,filter-debug.properties、filter-dev.properties、filter-product.properties,其中使用预设的环境参数

    1. #Environment
    2. environment=pro
    3. api.host=https://baidu.com/

    3、配置POM,完成resources、filters的配置以及插件的配置

    1. <build>
    2. <resources>
    3. <resource>
    4. <directory>src/main/resources</directory>
    5. <filtering>true</filtering>
    6. <!--扫描替换参数的文件路径-->
    7. </resource>
    8. </resources>
    9. <!--配置filter指向的文件,其中${env}为使用profile进行文件控制-->
    10. <filters>
    11. <filter>src/main/filters/filter-${env}.properties</filter>
    12. <!--环境过滤器的配置方式,回头需要在该路径下建立对应文件-->
    13. </filters>
    14. <plugins>
    15. <plugin>
    16. <!--该插件是解决命令下执行mvn test指定testng xxx.xml 文件 的配置-->
    17. <groupId>org.apache.maven.plugins</groupId>
    18. <artifactId>maven-surefire-plugin</artifactId>
    19. <version>2.22.0</version>
    20. <configuration>
    21. <!--为了解决在jenkins maven执行test 报告乱码问题,编码格式设置为UTF-8-->
    22. <argLine>-Dfile.encoding=UTF-8</argLine>
    23. <encoding>UTF-8</encoding>
    24. <!--动态指定执行的xml文件。${project.basedir}项目目录,${xmlFileName}maven文件-->
    25. <suiteXmlFiles>
    26. <!-- <suiteXmlFile>${project.basedir}/target/classes/testNg/${xmlFileName}</suiteXmlFile>-->
    27. <!--在IEDA上运行maven test命令时,可打开该注释使用完整测试集合-->
    28. <!--<suiteXmlFile>${project.basedir}/target/classes/testNg/api/APICollection-TestSuite.xml</suiteXmlFile>-->
    29. </suiteXmlFiles>
    30. </configuration>
    31. </plugin>
    32. <!--maven-compiler-plugin 插件是来对 Java 代码编译的,指定jdk版本-->
    33. <plugin>
    34. <groupId>org.apache.maven.plugins</groupId>
    35. <artifactId>maven-compiler-plugin</artifactId>
    36. <version>3.1</version>
    37. <configuration>
    38. <encoding>UTF-8</encoding>
    39. <source>8</source>
    40. <target>8</target>
    41. </configuration>
    42. </plugin>
    43. </plugins>
    44. </build>

    4、配置profile

    1. <profiles>
    2. <!-- 开发环境,默认激活 -->
    3. <profile>
    4. <id>dev</id>
    5. <properties>
    6. <env>dev</env>
    7. </properties>
    8. </profile>
    9. <!-- 生产环境 -->
    10. <profile>
    11. <id>pro</id>
    12. <properties>
    13. <env>pro</env>
    14. </properties>
    15. </profile>
    16. <!-- 测试环境 -->
    17. <profile>
    18. <id>fat</id>
    19. <properties>
    20. <env>fat</env>
    21. </properties>
    22. <activation>
    23. <activeByDefault>true</activeByDefault><!--默认启用的是fat环境配置-->
    24. </activation>
    25. </profile>
    26. </profiles>