gpt4 book ai didi

java - Maven Profile 从 Properties 文件读取数据

转载 作者:行者123 更新时间:2023-12-02 12:03:13 24 4
gpt4 key购买 nike

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

开发属性:

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");

任何帮助将不胜感激。

谢谢!

最佳答案

如果您只想根据命令行参数读取不同的属性文件,您是否可以只传递一个字符串,例如 -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 等

希望有帮助吗?

关于java - Maven Profile 从 Properties 文件读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47097316/

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