有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用maven pom文件中的外部文件来设置系统属性

我正在寻找最好的方法,这样我就可以导入一个。maven pom文件中的属性文件,该文件将在属性文件中找到的属性设置为系统属性

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

我遇到了maven properties插件,但这并没有将文件中的属性设置为系统属性,以下是该插件在我的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报告目标时,我使用的是系统。getProperties(“prop1”);,例如,我总是被返回null,这导致我的测试失败。这在maven中可能吗?我走对了吗


共 (1) 个答案

  1. # 1 楼答案

    我建议使用同一个插件的目标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>