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

Maven Profile属性获取始终返回null问题求助

解决Maven Profile属性无法通过System.getProperty()获取的问题

Maven Profile中定义的属性属于Maven构建属性,默认不会自动注入到JVM的系统属性中,因此直接调用System.getProperty()会返回null。要解决这个问题,需要通过Maven插件将Maven属性映射为JVM系统属性,让测试代码能够读取到。

解决方案步骤

1. 修改pom.xml,配置Maven Surefire插件传递属性

Maven Surefire插件负责运行测试用例,我们需要在插件配置中指定要传递给测试JVM的系统属性。你可以选择两种配置方式:

方式一:每个Profile单独配置插件(适合不同Profile需要不同插件参数的场景)

<profiles>
  <profile>
    <id>australia</id>
    <properties>
      <country.code>AU</country.code>
      <capital>Canberra</capital>
      <currency>AUD</currency>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.2.5</version> <!-- 使用最新稳定版本 -->
          <configuration>
            <!-- 将Maven属性映射为JVM系统属性 -->
            <systemPropertyVariables>
              <country.code>${country.code}</country.code>
              <capital>${capital}</capital>
              <currency>${currency}</currency>
            </systemPropertyVariables>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>

  <profile>
    <id>newzealand</id>
    <properties>
      <country.code>NZ</country.code>
      <capital>Wellington</capital>
      <currency>NZD</currency>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.2.5</version>
          <configuration>
            <systemPropertyVariables>
              <country.code>${country.code}</country.code>
              <capital>${capital}</capital>
              <currency>${currency}</currency>
            </systemPropertyVariables>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

方式二:全局配置插件,Profile仅定义属性(更简洁,推荐)

将Surefire插件配置放在全局build节点下,所有Profile共享该配置,只在Profile中定义属性即可:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.2.5</version>
      <configuration>
        <systemPropertyVariables>
          <country.code>${country.code}</country.code>
          <capital>${capital}</capital>
          <currency>${currency}</currency>
        </systemPropertyVariables>
      </configuration>
    </plugin>
  </plugins>
</build>

<profiles>
  <profile>
    <id>australia</id>
    <properties>
      <country.code>AU</country.code>
      <capital>Canberra</capital>
      <currency>AUD</currency>
    </properties>
  </profile>

  <profile>
    <id>newzealand</id>
    <properties>
      <country.code>NZ</country.code>
      <capital>Wellington</capital>
      <currency>NZD</currency>
    </properties>
  </profile>
</profiles>

2. IntelliJ运行测试的配置注意事项

  • 确保你的Run Configuration基于Maven配置创建:右键点击测试类 → Run 'TestClass' → 打开Run Configuration编辑窗口,检查Build and run部分是否选择了对应的Maven Profile。
  • 若需临时测试,也可在Run Configuration的VM options中手动添加属性(不推荐,易与pom配置冲突):比如-Dcountry.code=AU -Dcapital=Canberra -Dcurrency=AUD。

3. 命令行运行验证

使用你原来的命令即可,此时属性会通过Surefire插件传递到测试JVM:

mvn clean install -P australia test -DskipTests=false

或者直接运行测试阶段:

mvn test -P australia

4. 测试代码验证

测试代码中直接调用System.getProperty()即可获取对应属性:

@Test
public void testCountryProperties() {
    String countryCode = System.getProperty("country.code");
    String capital = System.getProperty("capital");
    String currency = System.getProperty("currency");
    
    // 断言验证(以Australia为例)
    assertEquals("AU", countryCode);
    assertEquals("Canberra", capital);
    assertEquals("AUD", currency);
}

内容的提问来源于stack exchange,提问作者Sameera De Silva

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 07:40:25