为`mvn exec:java`设置Java属性

时间:2013-01-11 00:00:03

标签: java maven

Maven的exec:java目标在与Maven本身相同的JVM中运行。

我希望能够将一些属性传递给java二进制文件(特别是-ea -Djava.util.logging.config.file=logging.properties),但是如何做到这一点并不明显。

注意:我想将属性传递给JVM,而不是应用程序的参数。理想情况下,我希望能够在pom.xml中指定这些,但我意识到由于Maven的启动可能不太可能。作为一种变通方法,设置所有类路径等的exec:exec目标就像我调用exec:java一样好。

2 个答案:

答案 0 :(得分:4)

来自usage page

    <configuration>
      <mainClass>com.example.Main</mainClass>
      <arguments>
        <argument>argument1</argument>
        ...
      </arguments>
      <systemProperties>
        <systemProperty>
          <key>java.util.logging.config.file</key>
          <value>logging.properties</value>
        </systemProperty>
        ...
      </systemProperties>
    </configuration>

必须在env变量MAVEN_OPTS

中设置其他JVM选项
MAVEN_OPTS=-ea 

答案 1 :(得分:0)

使用命令行:

call mvn exec:java -Dexec.classpathScope="test" -Dexec.mainClass="com.mycompany.MyFirstTest" -DPROPERTY_FILE="MyPropertyFile"

运行你的程序。

有一个经理班,负责阅读这些房产。

String loggingValue = MyPropertyManager.LOGGING.getPropertyValue();

然后,编写MyPropertyManager类以从属性文件加载属性。

public enum MyPropertyManager {
    LOGGING, OTHERPROPERTY, OTHER;

    public String getPropertyValue() {
        String propertyFile = System.getProperty("PROPERTY_FILE");
        // ... load property file
        Properties loadedProperties = .....
        return properties.get(LOGGING.toString());
    }
}

改进代码,以便只加载一次属性文件。

相关问题