Maven 2.1.0不会将系统属性传递给Java虚拟机

我们使用命令行将系统属性传递给Java虚拟机,当运行我们的Hudson在Linux机器上构build时。 它曾经在2.0.9中工作得很好,因为我们升级到2.1.0就完全停止了工作。 系统属性从来没有把它交给Java虚拟机。

我创build了一个小testing项目,实际上它根本不起作用。

这应该适用于Maven 2.0.9:

mvn2.0.9 -Dsystem.test.property=test test 

但是这将失败:

 mvn2.1 -Dsystem.test.property=test test 

Java代码简单地做到这一点

 assertTrue( System.getProperty("system.test.property") != null); 

我不认为这是Maven或Surefire插件的问题。 否则,绝对会有不同的performance。 看起来像现在,当Surefire分叉JVM时 ,不会从父JVM获取所有的系统属性。

这就是为什么你应该使用argLine来传递你想要testing的任何系统属性。 所以,这两个应该工作

 mvn2.1 -Dsystem.test.property=test test -DforkMode=never 

要么

 mvn2.1 test -DargLine="-Dsystem.test.property=test" 

我已经经历了与Surefire插件。 Surefire插件在由Maven启动的另一个JVM实例下运行。 命令行参数可以在pom.xml中的surefile-pluginconfiguration下configuration。 这是我们的configuration示例。

  <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.3</version> <!-- By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns: "**/Test*.java" - includes all of its subdirectory and all java filenames that start with "Test". "**/*Test.java" - includes all of its subdirectory and all java filenames that end with "Test". "**/*TestCase.java" - includes all of its subdirectory and all java filenames that end with "TestCase". --> <configuration> <includes> <include>**/*Test.java</include> </includes> <systemProperties> <property> <name>app.env</name> <value>dev</value> </property> <property> <name>oracle.net.tns_admin</name> <value>${oracle.net.tns_admin}</value> </property> </systemProperties> </configuration> </plugin> 

注意不要将configuration文件与命令行参数混淆。 configuration文件(pom.xml)覆盖了所有的cmd参数。 所以如果你想通过像raisercostin解释的命令行来传递它,请不要在pom.xml中configurationsurefire插件。