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

Maven多环境配置:properties引用pom变量执行Serenity测试失败

Fixing Maven Profile Property Substitution for Serenity Tests

Let’s break down why your property substitution isn’t working and walk through the fixes step by step.

The Root Cause

The biggest issue here is that your Java code is reading the original unfiltered file directly from src/main/resources, instead of the processed (filtered) version Maven generates in the target/classes directory. That’s why you’re seeing ${webdriver.base.url} instead of the actual value from your staging profile.

Step 1: Load Properties from the Classpath

Instead of using FileInputStream to read the source file, load the properties from the classpath. This ensures you get the filtered version Maven has processed and copied to your build output.

Update your Java code to this:

Properties properties = new Properties();
try (InputStream in = getClass().getClassLoader().getResourceAsStream("profile.properties")) {
    if (in == null) {
        throw new FileNotFoundException("profile.properties not found in classpath");
    }
    properties.load(in);
} catch (Exception e) {
    e.printStackTrace();
}
String baseUrl = properties.getProperty("url");
System.out.println(baseUrl + "<---");
driver.get(baseUrl);

Using getResourceAsStream from the classloader will automatically pick up the filtered file from target/classes after Maven runs its resource processing phase.

Step 2: Verify Maven Resource Filtering Configuration

Your existing resource setup looks mostly correct, but let’s double-check to ensure filtering is applied properly:

  • The first resource block enables filtering for profile.properties in src/main/resources (this is what replaces ${webdriver.base.url} with the profile value)
  • The second block disables filtering for all other resources (good practice to avoid unintended changes to other files)

Make sure your base <properties> section in the pom has a default value (or leave it empty—your staging profile will override it):

<properties>
    <webdriver.base.url></webdriver.base.url>
</properties>

Step 3: Ensure the Profile is Activated

Your staging profile is set to <activeByDefault>true</activeByDefault>, so it should activate automatically. But if you want to explicitly activate it (or switch to another profile like prod later), run Maven with:

mvn clean test -P staging

For a production profile (once you add it), you’d use:

mvn clean test -P prod

Step 4: Test the Setup

After making these changes:

  1. Run mvn clean to clear any old build artifacts
  2. Run mvn test (or the profile-specific command)
  3. Check the console output—you should now see google.de<--- instead of the placeholder

Additional Serenity-Specific Tip

Serenity has its own built-in configuration system that can simplify this process. Instead of using a custom profile.properties file, you can override Serenity’s base URL directly in your Maven profiles:

<profile>
    <id>staging</id>
    <properties>
        <serenity.base.url>google.de</serenity.base.url>
    </properties>
</profile>

Then in your code, you can get the value using Serenity’s built-in method:

String baseUrl = Serenity.getConfiguration().getBaseUrl();
driver.get(baseUrl);

This is more idiomatic for Serenity tests and eliminates the need for a custom properties file.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:09:03