调用EXEC时如何传递SystemProperties:Maven中的Java插件?
-
02-10-2019 - |
题
我想使用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>
解决方案
没有办法设置 <systemProperties>
范围 在命令行上。
但是,自从 exec:java
不是分叉的,您只能将系统属性传递给Maven,它将被它捡起 exec:java
也是。
mvn -Dkey=value exec:java -Dexec.mainClass=com.yourcompany.yourclass \
-Dexec.args="arg1 arg2 arg3"
其他提示
尝试跟随我,它正常工作
<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>
我只是遇到了一个类似的问题,我想为可能遇到这个问题的其他人写一个完整的答案。
即使问题与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
它应该以相同的方式工作。
不隶属于 StackOverflow