java 在 exec-maven-plugin 中设置 systemProperty 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36574248/
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
set systemProperty in exec-maven-plugin does not work
提问by Tran Ngu Dang
Here is my pom.xml file:
这是我的 pom.xml 文件:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<profiles>
<profile>
<id>my_proj</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>com.test.Main</argument>
</arguments>
<systemProperties>
<systemProperty>
<key>someKey</key>
<value>someValue</value>
</systemProperty>
</systemProperties>
<environmentVariables>
<someKey>
someValue
</someKey>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
and in Main.java
并在 Main.java 中
public static void main(String[] args) {
System.out.println("hello world" + System.getenv("someKey") + " " + System.getProperty("someKey"));
}
the output when I run
我运行时的输出
mvn install -Pmy_proj
is
是
hello worldsomeValue null
I cannot seem to get the systemProperty value. What did I do wrong ?
我似乎无法获得 systemProperty 值。我做错了什么 ?
回答by A_Di-Matteo
The systemProperty
does not work simply because it is not an expected element of the exec
goal of the exec-maven-plugin
.
的systemProperty
,因为它不是的预期元素不只是工作exec
的目标exec-maven-plugin
。
Check the official exec
goal page, no systemProperties
element is specified. As such, your configuration is still valid for Maven simply because is well-formed XML, but it is ignored by the exec-maven-plugin
.
检查官方exec
目标页面,没有systemProperties
指定元素。因此,您的配置对 Maven 仍然有效,因为它是格式良好的 XML,但它被exec-maven-plugin
.
From official Maven Pom Referenceconcerning the plugin configuration
element:
来自关于插件元素的官方Maven Pom 参考configuration
:
It may be good to note that all configuration elements, wherever they are within the POM, are intended to pass values to another underlying system, such as a plugin. In other words: values within a configuration element are never explicitly required by the POM schema, but a plugin goal has every right to require configuration values.
最好注意所有配置元素,无论它们位于 POM 中的哪个位置,都旨在将值传递给另一个底层系统,例如插件。换句话说:POM 模式从不明确要求配置元素中的值,但插件目标完全有权要求配置值。
You are making confusion with the systemProperties
configuration entry foreseen by its java
goal. This option is available there because of its context: it's crafted for java executions. On the other hand, the exec
goal is much more generic and as such cannot foresee an option required only by java programs.
您正在混淆systemProperties
其java
目标所预见的配置条目。这个选项在那里可用是因为它的上下文:它是为 java 执行而设计的。另一方面,exec
目标更加通用,因此无法预见仅 Java 程序需要的选项。
To pass system properties to a Java execution via the exec
goal, you can use the arguments
configuration entry and use the -D
notation
要通过exec
目标将系统属性传递给 Java 执行,您可以使用arguments
配置条目并使用-D
符号
-Dproperty=value
Sets a system property value.
-Dproperty=value
设置系统属性值。
Further note, as per official Running Java programs with the exec goaldocumentation, the -D
arguments should go first:
进一步注意,根据带有 exec 目标文档的官方运行 Java 程序,-D
参数应该放在最前面:
<configuration>
<executable>java</executable>
<arguments>
<argument>-DsomeKey2=someValue2</argument>
<argument>-classpath</argument>
<classpath />
<argument>com.test.Main</argument>
</arguments>
<environmentVariables>
<someKey>someValue</someKey>
</environmentVariables>
</configuration>
Moreover, you should not set the same variable name for environment and as system property, the system property would not be set otherwise.
此外,您不应该为环境和系统属性设置相同的变量名,否则系统属性将不会被设置。