You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Maven Profiles读取对应.properties文件执行测试?

问题

我在pom.xml中配置了两个Maven Profile,用于指定不同的配置文件路径:

<profiles>
    <profile>
        <id>urlOne</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <systemPropertyVariables>
                            <env>local</env>
                            <properties.file>src/main/resources/urlOne.properties</properties.file>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>urlTwo</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <systemPropertyVariables>
                            <env>local</env>
                            <properties.file>src/main/resources/urlTwo.properties</properties.file>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

src/main/resources目录下有两个配置文件:

  • urlOne.properties内容:url=https://www.pageOne.com/
  • urlTwo.properties内容:url=https://www.pageTwo.com/

Hook类现有代码如下:

@Before()
public void before(Scenario scenario) {
    System.setProperty("webdriver.chrome.driver", "C://Users//User//Projects/chromedriver.exe");
    driver = new ChromeDriver();

    Properties properties = new Properties();
    InputStream inputStream = new FileInputStream(PROPERTIES_FILE_NAME);
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
    properties.load(inputStreamReader);
    driver.get(properties.getProperty("url"));
}

希望执行mvn test -PurlOne时读取urlOne.properties打开对应页面,执行mvn test -PurlTwo时读取urlTwo.properties打开对应页面,请问如何修改代码实现该需求?

解决方案

1. 核心思路

Maven Profile已经通过maven-surefire-plugin将配置文件路径以系统属性(properties.file)的形式传递给测试代码,只需在Java代码中读取这个系统属性,即可动态获取对应配置文件的路径。

2. 修改Hook类代码

替换固定的PROPERTIES_FILE_NAME,改为读取系统属性properties.file,同时增加空值校验避免运行时异常:

@Before()
public void before(Scenario scenario) {
    System.setProperty("webdriver.chrome.driver", "C://Users//User//Projects/chromedriver.exe");
    driver = new ChromeDriver();

    Properties properties = new Properties();
    // 读取Maven传递的系统属性
    String propertiesFilePath = System.getProperty("properties.file");
    if (propertiesFilePath == null || propertiesFilePath.isEmpty()) {
        throw new IllegalArgumentException("未指定配置文件路径,请通过Maven Profile指定");
    }
    try (InputStream inputStream = new FileInputStream(propertiesFilePath);
         InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
        properties.load(inputStreamReader);
        driver.get(properties.getProperty("url"));
    } catch (IOException e) {
        throw new RuntimeException("加载配置文件失败", e);
    }
}

3. 关键说明

  • 使用System.getProperty("properties.file")直接读取Maven传递的系统属性,该属性的值就是Profile中配置的src/main/resources/xxx.properties路径;
  • 增加空值校验,防止未指定Profile时出现文件找不到的错误;
  • 使用try-with-resources语法自动关闭流资源,避免资源泄漏;
  • 捕获IO异常并包装为运行时异常,便于快速定位问题。

内容的提问来源于stack exchange,提问作者Word Place

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 12:02:11