25

我想使用 exec:java 插件从命令行调用主类。我可以使用命令行从命令行传递参数-Dexec.args="arg0 arg1 arg2",但我不知道如何传递系统属性。我试过 '-Dexec.systemProperties="key=value"` 但没有效果。

pom.xml看起来像这样:

  <plugin>  
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
      <mainClass>ibis.structure.Structure</mainClass>
    </configuration>  
  </plugin>
4

3 回答 3

26

尝试为我关注它可以正常工作

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
                <mainClass>ibis.structure.Structure</mainClass>
                <systemProperties>
                    <systemProperty>
                        <key>someKey</key>
                        <value>someValue</value>
                    </systemProperty>
                </systemProperties>
            </configuration>
        </plugin>
于 2014-02-18T20:53:04.103 回答
18

无法在命令行上设置<systemProperties> 参数。

但是,由于exec:java没有分叉,因此您只需将系统属性传递给 maven,它也会被拾取exec:java

mvn -Dkey=value exec:java -Dexec.mainClass=com.yourcompany.yourclass \
    -Dexec.args="arg1 arg2 arg3"
于 2010-09-14T12:39:10.267 回答
7

我刚刚遇到了类似的问题,我想为可能遇到这个问题的其他人写一个完整的答案。

即使问题不是关于 pom.xml 而是关于命令行 - 它没有说明如何对 pom.xml 做同样的事情,所以在这里

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>

                <goals>
                    <goal>java</goal>
                </goals>

                <configuration>
                     <mainClass>myPackage.MyMain</mainClass>
                      <systemProperties>
                          <property>
                              <key>myKey</key>
                              <value>myValue</value>
                          </property>
                      </systemProperties>
                </configuration>

            </plugin>
        </plugins>
    </build>

对于命令行-我认为Sean Patrick Floyd's答案很好-但是,如果您在 pom.xml 中已经定义了某些内容,它将覆盖它。

所以跑步

 mvn exec:java -DmyKey=myValue

也应该为你工作。

您还应该注意exec 插件的文档说明了以下内容

A list of system properties to be passed. 
Note: as the execution is not forked, some system properties required 
by the JVM cannot be passed here. 
Use MAVEN_OPTS or the exec:exec instead. See the user guide for more information.

所以你也可以做这样的事情

export MAVEN_OPTS=-DmyKey=myValue
mvn exec:java

它应该以同样的方式工作。

于 2013-08-11T13:38:55.033 回答