在exec-maven-plugin中设置systemProperty不起作用 [英] set systemProperty in exec-maven-plugin does not work
问题描述
这是我的pom.xml文件:
Here is my pom.xml file:
<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>
和Main.java
and in Main.java
public static void main(String[] args) {
System.out.println("hello world" + System.getenv("someKey") + " " + System.getProperty("someKey"));
}
我运行时的输出
mvn install -Pmy_proj
是
hello worldsomeValue null
我似乎无法获得systemProperty值.我做错了什么?
I cannot seem to get the systemProperty value. What did I do wrong ?
推荐答案
systemProperty
不能仅仅因为它不是 exec-maven-plugin
.
The systemProperty
does not work simply because it is not an expected element of the exec
goal of the exec-maven-plugin
.
检查正式的 exec
目标页面,否指定了systemProperties
元素.这样,您的配置仍然对Maven仍然有效,只是因为它是格式正确的XML,但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
.
有关插件configuration
元素的官方 Maven Pom参考:
可能需要注意的是,所有配置元素,无论它们位于POM内的什么地方,都旨在将值传递给另一个基础系统,例如插件.换句话说:POM模式从不明确要求配置元素中的值,但是插件目标具有要求配置值的所有权利.
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.
您对 systemProperties
通过其 java
目标预见的配置条目.此选项因其上下文而在此处可用:它是为Java执行而设计的.另一方面,exec
目标更加通用,因此无法预见仅Java程序需要的选项.
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.
要通过exec
目标将系统属性传递给Java执行,可以使用 arguments
配置条目,并使用 -D
表示法
To pass system properties to a Java execution via the exec
goal, you can use the arguments
configuration entry and use the -D
notation
-Dproperty=value
设置系统属性值.
-Dproperty=value
Sets a system property value.
根据官方使用exec目标文档运行Java程序,应首先使用-D
参数:
Further note, as per official Running Java programs with the exec goal documentation, the -D
arguments should go first:
<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.
这篇关于在exec-maven-plugin中设置systemProperty不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!