1.5 jenkins与pom间的参数传递

一、先在pom的<properties>标签内添加env参数,在@Test方法下添加

System.out.println(System.getProperty("env"));
 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>15</maven.compiler.source>
    <maven.compiler.target>15</maven.compiler.target>
    <env>test</env>
  </properties>

由此得知,System.getProperty方法并不能获取properties标签中值。

二、注释掉properties标签的新增的变量,在maven-surefire-plugin下的<configuration>标签添加env,并执行

 <systemPropertyVariables>
              <env>pre</env>
            </systemPropertyVariables>

结果:控制台输出了pre,由此得知,getProperty方法能够获取到systemPropertyVariables的值。

三、运行参数中添加env

mvn clean test -Dsurefire.suiteXmlFiles=src/test/java/org/example/suite/testng.xml  -Denv=pre2

结果:如果env变量名存在于systemPropertyVariables中,则会输出pre2,如果命令行中参数名不是env,且systemPropertyVariables中存在env,

则输出systemPropertyVariables中的env值。

四、properties标签中设置env,同时在systemPropertyVariables中引用

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>15</maven.compiler.source>
    <maven.compiler.target>15</maven.compiler.target>
    <env>test</env>
  </properties>
<systemPropertyVariables>
              <env>${env}</env>
            </systemPropertyVariables>

结果:正确输出env的值

 

综述:

a.properties标签中定义的变量只供systemPropertyVariables标签中引用,且两处的标签要一致

b.命令行上追加的参数,实际上是直接改变systemPropertyVariables中的值

c.jenkins中定义的String Parameters,实际上在shell构建中追加到mvn命令行的,即是直接给systemPropertyVariables中的参数传参

d.properties中相当于systemPropertyVariables中的初始化值,而传参,相当于重新赋值。

那suiteXmlFile标签中是否可以引用在properties标签中定义的默认值呢?

1、在properties中增加suite标签,值是testng.xml

2、在suiteXmlFile中引用,提交到gitlab,构建jenkins

结果:执行成功

结语:

1、Jenkins中新建String parameters可以通过${name}或者$name方式引用,name是变量名

2、参数构建场景可以用于接口地址和环境切换上,在properties和systemPropertyVariables设定默认接口地址,

代码中通过System.getProperty()方法来判断是否切换接口。当然也可以在代码中配置不同的地址,通过命令行

传递自定义的变量判断是否切换接口地址

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值