【问题标题】:Reference tag from pom.xml in properties file to use it in Java在属性文件中引用 pom.xml 中的标记以在 Java 中使用它
【发布时间】:2018-01-25 15:41:49
【问题描述】:

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}

马文:

...
<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>
...

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

PS 当我将鼠标悬停在属性文件中的${webdriver.base.url} 上并按Ctrl 并单击它时,我将被重定向到xml 文件到&lt;webdriver.base.url&gt;google.de&lt;/webdriver.base.url&gt;

【问题讨论】:

    标签: java maven properties serenity-bdd


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2017-09-07
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      相关资源
      最近更新 更多