简体   繁体   English

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

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

I want to use the exec:java plugin to invoke the main class from command line. 我想使用exec:java插件从命令行调用主类。 I can pass arguments from the command line using -Dexec.args="arg0 arg1 arg2" , I don't know how to pass system properties. 我可以使用-Dexec.args="arg0 arg1 arg2"从命令行传递参数,我不知道如何传递系统属性。 I tried '-Dexec.systemProperties="key=value"` but with no effect. 我试过'-Dexec.systemProperties =“key = value”`但没有效果。

pom.xml looks like this: pom.xml看起来像这样:

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

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>

There is no way to set the <systemProperties> parameter on the command line. 无法在命令行上设置<systemProperties> 参数

However, since exec:java is not forked, you can just pass a system property to maven and it will be picked up by exec:java as well. 但是,由于exec:java不是分叉的,你可以只将系统属性传递给maven,它也会被exec:java选中。

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

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's answer 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 documentations states 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. 它应该以同样的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自Maven故障安全插件和Spock测试的SystemProperties - SystemProperties from maven failsafe plugin with Spock Tests 运行Maven exec插件时Java的版本错误 - Bad version of java when running maven exec plugin Maven exec 插件 - 当 classpathscope=test 时如何从 main/java 导入类 - Maven exec plugin- how to import classes from main/java when classpathscope=test 运行 exec-maven-plugin java 时如何获取“[DEBUG] Executing command line:” - How to get “[DEBUG] Executing command line:” when running exec-maven-plugin java 如何在exec-maven-plugin中手动指定java路径 - How to manually specify java path in exec-maven-plugin 你能为maven exec插件定义exec和java目标吗? - Can you define both an exec and java goal for maven exec plugin? 使用 Exec Maven 插件分叉 Java,而不使用 `exec` 目标 - Forking Java using the Exec Maven Plugin, without using the `exec` goal 如何传递gradle systemProperties JUnit5测试? - How to pass gradle systemProperties JUnit5 tests? 调用方法时如何绕过Java中的继承 - How to by-pass inheritance in java when invoking a method 如何使用插件依赖项和 java 模块系统修复 exec-maven-plugin? - How to fix exec-maven-plugin with plugin dependencies and java module-system?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM