基本概念说明(resources、filter和profile):
profiles定义了各个环境的变量id
filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值
resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下,像db,bean等,里面用到的变量在打包时就会根据filter中的变量配置替换成固定值
在做自动化测试时,往往需要对多套环境进行测试,但测试环境,开发环境,生产环境的域名等配置往往是不一致的,自动化代码里的域名手动修改切换过于麻烦无法在jenkins自动集成构建,利用maven的profile和filter插件可以实现打包编译时指定需要的环境,从而实现不同环境的自动化集成测试
一,原理:
利用filter实现对资源文件(resouces)过滤
maven filter可利用指定的xxx.properties中对应的key=value对资源文件中的
k
e
y
进
行
替
换
,
最
终
把
你
的
资
源
文
件
中
的
u
s
e
r
n
a
m
e
=
{key}进行替换,最终把你的资源文件中的username=
key进行替换,最终把你的资源文件中的username={key}替换成username=value
利用profile来切换环境
maven profile可使用操作系统信息,jdk信息,文件是否存在,属性值等作为依据,来激活相应的profile,也可在编译阶段,通过mvn命令加参数 -PprofileId 来手工激活使用对应的profile
结合filter和profile,我们就可以方便的在不同环境下使用不同的配制
二,配置:
在pom.xml中配置3个profile,对应项目所处的3个不同的环境dev, test, product, profile的id属性即为每个环境赋予一个唯一的标示,元素的内容则是以key-value的形式出现的键值对,如我们定义了一个变量,其值在不同的环境下(不同id)被赋予了不同的值(dev, product, test),要激活不同的环境打包,我们可以在命令行通过mvn package –P${profileId}来让其运行,为了开发便利,默认激活的是test测试环境,即不需要通过命令行手动输入-p参数也能运行test环境的打包。
1.定义profile
<build>
<resources>
<resource>
<!--配置需要被替换的资源文件地址-->
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<!--环境过滤器的配置方式,回头需要在该路径下建立对应文件-->
<filter>src/main/filters/filter-${env}.properties</filter>
</filters>
<plugins>
<!-- 解决filters替换失败问题的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
<delimiters>
<delimiter>${*}</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
<!-- 指定JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- 测试代码打包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<suiteXmlFiles>
./src/main/resources/testng.xml
</suiteXmlFiles>
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
<encoding>UTF-8</encoding>
<systemProperties>
<property>
<name>net.sourceforge.cobertura.datafile</name>
<value>target/cobertura/cobertura.ser</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
<!-- 不同的打包环境 -->
<profiles>
<!-- 开发环境,默认激活 -->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>product</id>
<properties>
<env>product</env>
</properties>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault><!--默认启用的是test环境配置-->
</activation>
</profile>
</profiles>
2.定义filter
可以使用Maven来对项目资源进行变量替换。在资源过滤被激活的时候,Maven会扫描
资源,寻找由${}包围的Maven属性的引用。一旦它找到这些引用,它就会使用合适的值去替换它们,当需要根据目标部署平台使用不同的配置来参数化一个构建的时候,这就非常有用。
1.在resources文件夹下新增env.properties文件
在main文件夹下新建filters文件夹,在filters文件夹下新建filter-dev.properties,filter-product.properties,filter-test.properties文件,配置需要替换的信息
二,使用:
1.使用mvn clean test命令默认会执行test环境配置
2.使用mvn clean test -Pdev命令会执行dev环境配置
2.使用mvn clean test -Pproduct命令会执行product环境配置