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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3708846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:05:21  来源:igfitidea点击:

How to pass systemProperties when invoking exec:java plugin in maven?

javamaven-2exec-maven-plugin

提问by Alexandru

I want to use the exec:java plugin to invoke the main class from command line. I can pass arguments from the command line using -Dexec.args="arg0 arg1 arg2", I don't know how to pass system properties. I tried '-Dexec.systemProperties="key=value"` but with no effect.

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

pom.xmllooks like this:

pom.xml看起来像这样:

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

采纳答案by Sean Patrick Floyd

There is no way to set the <systemProperties>parameteron the command line.

没有办法在命令行上设置<systemProperties>参数

However, since exec:javais not forked, you can just pass a system property to maven and it will be picked up by exec:javaas well.

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

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

回答by mokshino

Try following for me it works properly

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

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

回答by guy mograbi

I just ran into a similar problem and I wanted to write a full answer for others that might come across this question.

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

Even though the question is not about pom.xml but about command line - it does not state how to do the same with pom.xml so here it is

即使问题不是关于 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>

For the command line - I think Sean Patrick Floyd'sanswer is good - however, if you have something already defined in your pom.xml it will override it.

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

So running

所以跑步

 mvn exec:java -DmyKey=myValue

should also work for you.

也应该为你工作。

You should also note that the exec plugin's documentationsstates the following

您还应该注意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.

So you can also do something like this

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

export MAVEN_OPTS=-DmyKey=myValue
mvn exec:java

and it should work the same way.

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