【问题标题】:Maven Profile reads data from Properties fileMaven Profile 从 Properties 文件中读取数据
【发布时间】:2017-11-03 13:47:05
【问题描述】:

我有一个使用 Maven 作为构建工具的 Selenium 项目,我想从 .properties 文件中读取不同的环境详细信息(协议、域、子域等)。是否可以使用 Maven 配置文件,以便根据我在触发 mvn 命令时指定的配置文件在不同的环境(例如 dev、staging、prod)上运行我的测试?

dev.properties:

protocol=http
domain=domain.com
subdomain=www

pom.xml:

  <profiles>
    <profile>
      <id>prod</id>
      <activation>
       <activeByDefault>true</activeByDefault>
      </activation>
    </profile>
    <profile>
      <id>pre_prod</id>
    </profile>
    <profile>
      <id>dev</id>
    </profile>
  </profiles>

mvn:

mvn clean test -Pdev

然后应该在 java 代码中使用

检索数据
System.getProperty("protocol");
System.getProperty("domain");
System.getProperty("subdomain");

非常感谢任何帮助。

谢谢!

【问题讨论】:

    标签: java maven selenium properties maven-profiles


    【解决方案1】:

    如果你只是想根据命令行参数读取不同的属性文件,你能不能只传入一个字符串,例如 -Denv=dev

    然后在你的属性阅读器类中让它基于 System.getProperty("env"); 初始化属性文件;

    Properties generalProperties = new Properties();
        String generalPropertiesFileName = "data/"+ 
          System.getProperty("env") + ".properties";
       initProperties(generalProperties, generalPropertiesFileName);
    

    或者,您也可以以相同的方式将属性从命令行传递到您的 POM -

    <properties>
      <property>
        <protocol></protocol>
        <domain></domain>
        <subdomain></subdomain>
      <property>
    <properties>
    

    然后这些可以作为 -Dprotocol=foo 等从命令行传入

    希望有帮助吗?

    【讨论】:

    • 嘿@JackForman,非常感谢您就此提出答案。直接在 pom.xml 文件中定义 来处理环境对于我的问题来说并不是一个合适的解决方案,因为该项目必须处理至少 10 个网站的测试,每个网站都有自己的开发、登台和产品配置。这样,pom 文件会变得非常混乱,因为其中会添加大量数据。最后,我的最终 maven 任务应该看起来像 mvn clean test -Dbrand=ebay -Pdev 其中“品牌”将是正在测试的网站之一。
    • 好的,第一个使用 props 文件获取命令行参数的解决方案可能适用于此?但是,您可能可以使用我不知道的配置文件做一些聪明的事情
    • 刚刚完成了您建议的第一个解决方案,效果很好!再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2011-10-04
    • 1970-01-01
    • 2014-06-29
    • 2013-10-03
    相关资源
    最近更新 更多