我刚刚遇到了类似的问题,我想为可能遇到这个问题的其他人写一个完整的答案。
即使问题不是关于 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
它应该以同样的方式工作。