gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:59:30 25 4
gpt4 key购买 nike

我有一个 selenium 脚本,我想从 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}"

最佳答案

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

关于java - 如何在 Jenkins 中使用构建参数编辑 app.properties 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55742416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com