如何在 Jenkins 中使用构建参数编辑 app.properties 文件

How to edit app.properties file with build parameter in Jenkins

我有一个 selenium 脚本,我想 运行 来自 Jenkins。我有一个名为 app.properties 的属性文件。此文件包含以下属性:

browser=chrome

我将我的项目配置为参数化,所以当我 运行 我的构建时,它要求浏览器参数。我想 select 这个参数(例如 firefox),这样它会在 app.properties 中更改浏览器 属性 并在 Firefox 中 运行 自动化。

通常,当我在 Intellij 中 app.properties 中更改浏览器 属性 时,我的程序 运行 使用该浏览器。所以从这个意义上说,我的程序没有任何问题。

有没有办法更改 app.properties 我的 Jenkins 构建参数和 运行 具有该配置的程序?

编辑:我找到了以下解决方案:

  1. 安装 surefire 插件。
  2. 添加浏览器参数。
  3. 在你的属性管理class中,将浏览器参数设为

    System.getProperty("browser");

  4. 从jenkins,配置一个浏览器参数

  5. 调用maven命令:mvn test "-Dbrowser=${BROWSER}"

“根据你选择的参数类型不同,参数生效的方式也不同...字符串参数暴露为同名环境变量。” https://wiki.jenkins.io/plugins/servlet/mobile?contentId=34930782#content/view/34930782

您可以通过系统属性来更改配置。 首先,您应该将项目配置为读取系统属性和配置文件,其中系统属性具有更高的优先级。我推荐 Apache Commons Composite Configuration。它可能看起来像这样:

CompositeConfiguration configuration = new CompositeConfiguration();
try {
  configuration.addConfiguration(new SystemConfiguration());
  configuration.addConfiguration(new PropertiesConfiguration("app.properties"));
} catch (ConfigurationException e) {
  e.printStackTrace();
}
//Read your configuration values here

这样,当您提供系统 属性 -Dbrowser=chrome 时,它将覆盖配置文件中的值。

其次,您需要配置 Jenkins 作业。由于您要传递参数,因此可以在构建步骤定义中使用它:

mvn clean test -Dbroswer=${browser-param}