gpt4 book ai didi

java - 在属性文件中引用 pom.xml 中的标记以在 Java 中使用它

转载 作者:行者123 更新时间:2023-12-02 11:42:31 25 4
gpt4 key购买 nike

Java:

    ...
Properties properties = new Properties();
FileInputStream in;
try {
in = new FileInputStream("src/main/resources/profile.properties");
properties.load(in);
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println(properties.getProperty("url") + "<---");
driver.get(properties.getProperty("url"));
...

profile.properties 文件:

url = ${webdriver.base.url}

Maven:

...
<properties>
...
<webdriver.base.url></webdriver.base.url>
...
</properties>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>profile.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>profile.properties</exclude>
</excludes>
</resource>
</resources>
...
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>staging</id>
<properties>
<webdriver.base.url>google.de</webdriver.base.url>
</properties>
</profile>
</profiles>
...

我想在不同的系统(暂存、产品等)中执行 Restful 测试。为此,我尝试使用 pom.xml 中的配置文件,引用 <webdriver.base.url>标记为值 urlprofile.properties ,然后通过driver.get(properties.getProperty("url"));调用这个值。 System.out.println(properties.getProperty("url") + "<---");给出${webdriver.base.url}<---在控制台中(expexted google.de )。所以,这意味着引用不起作用。我已经阅读了很多有关该主题的主题,但没有找到解决我的问题的方法。如何将 xml 中的标签引用到属性文件中的变量,然后在 Java 中使用它?可能吗?

PS,当我将鼠标悬停在 ${webdriver.base.url} 上时在属性文件中并按 Ctrl然后单击它,我将被重定向到 xml文件至<webdriver.base.url>google.de</webdriver.base.url> .

最佳答案

您正在使用 src/main/resources 中的文件,Maven 将替换的属性文件放入 ${project.build.directory} 中。所以你需要改变你的java代码来使用

in = ClassLoader.getSystemResourceAsStream("profile.properties");

关于java - 在属性文件中引用 pom.xml 中的标记以在 Java 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48446697/

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