4

我正在寻找可能的最佳方法,以便可以在 maven pom 文件中导入 .properties 文件,该文件将在属性文件中找到的属性设置为系统属性。

例如,我有一个 dev.properties 文件,其中包含特定于开发人员的系统属性,这意味着 dev.properties 文件不会保留在源代码存储库中。

我遇到了 maven 属性插件,但这并没有将文件中的属性设置为系统属性,这是我的 pom 文件中这个插件的声明:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
       <files>
           <file>${basedir}/dev.properties</file>
       </files>
    </configuration>
</plugin>

但是在我的 java 代码中,当我运行测试目标或 jacoco 报告目标时,我使用 System.getProperties("prop1");,例如,我总是返回 null,这导致我的测试失败。这在maven中可能吗?我在正确的轨道上吗?

4

1 回答 1

3

我建议使用同一插件的目标set-system-properties,或者您也可以使用通常的方式来设置系统属性,方法是在通过您的配置读取属性后使用这些属性进行单元测试:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
        <configuration>
          <systemPropertyVariables>
            <propertyName>propertyValue</propertyName>
            <buildDirectory>${project.build.directory}</buildDirectory>
            [...]
          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
于 2013-06-15T17:22:45.317 回答