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

问题描述 投票:0回答:4

我想使用 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>
java maven-2 exec-maven-plugin
4个回答
27
投票

尝试按照我的操作,它可以正常工作

        <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>

19
投票

无法在命令行上设置

<systemProperties>
参数

但是,由于

exec:java
没有分叉,您只需将系统属性传递给 Maven,它也会被
exec:java
拾取。

mvn -Dkey=value exec:java -Dexec.mainClass=com.yourcompany.yourclass \
    -Dexec.args="arg1 arg2 arg3"

8
投票

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

尽管问题不是关于 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

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


0
投票

对于目标执行者:

<artifactId>exec-maven-plugin</artifactId>
<executions>
    <execution>
        <goals>
            <goal>exec</goal>
        </goals>
        <configuration>
            <arguments>
                <argument>-Dsome.key=true</argument>
© www.soinside.com 2019 - 2024. All rights reserved.