如何在maven中调用exec:java插件时传递systemProperties?

Ale*_*dru 20 java maven-2 exec-maven-plugin

我想使用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>
Run Code Online (Sandbox Code Playgroud)

mok*_*ino 22

尝试跟我说它工作正常

        <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>
Run Code Online (Sandbox Code Playgroud)


Sea*_*oyd 15

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

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

mvn -Dkey=value exec:java -Dexec.mainClass=com.yourcompany.yourclass \
    -Dexec.args="arg1 arg2 arg3"
Run Code Online (Sandbox Code Playgroud)


guy*_*abi 6

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

即使问题不是关于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>
Run Code Online (Sandbox Code Playgroud)

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

好跑

 mvn exec:java -DmyKey=myValue
Run Code Online (Sandbox Code Playgroud)

也应该适合你.

您还应该注意,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.
Run Code Online (Sandbox Code Playgroud)

所以你也可以这样做

export MAVEN_OPTS=-DmyKey=myValue
mvn exec:java
Run Code Online (Sandbox Code Playgroud)

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