【问题标题】:How to edit app.properties file with build parameter in Jenkins如何在 Jenkins 中使用构建参数编辑 app.properties 文件
【发布时间】:2019-04-18 08:50:22
【问题描述】:

我有一个硒脚本,我想从 Jenkins 运行它。我有一个名为 app.properties 的属性文件。该文件包含以下属性:

browser=chrome

我将我的项目配置为参数化,因此当我运行构建时,它会询问浏览器参数。我想选择这个参数(例如 firefox),这样它会改变 app.properties 中的浏览器属性并在 Firefox 中运行自动化。

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

有没有办法根据我的 Jenkins 构建参数更改 app.properties 并使用该配置运行程序?

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

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

    System.getProperty("浏览器");

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

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

【问题讨论】:

  • 您必须将参数传递给您正在调用的 maven 任务 -Drun.jvmArguments="-Dbrowser=chrome"。并且可以使用变量名从詹金斯注入“chrome”值。

标签: java maven selenium jenkins testng


【解决方案1】:

您可以传递系统属性来更改配置。 首先,您应该将项目配置为读取系统属性和配置文件,其中系统属性将具有更高的优先级。我推荐 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}

【讨论】:

  • 我采用了另一种解决方案,但这也是一种非常好的方法。谢谢。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多