2009-05-05 90 views
38

在Linux机器上运行我们的Hudson时,我们使用命令行将系统属性传递给Java 虚拟机。它使用 在2.0.9中工作得很好,因为我们升级到2.1.0,所以 完全停止工作。系统属性永远不会将其设置为Java虚拟机的 。未将系统属性传递给Java虚拟机的Maven 2.1.0

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

这应该只是罚款与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); 

回答

52

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

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

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

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

“argLine”就是我一直在寻找的!非常感谢! – armandino 2010-04-12 22:49:47

+0

令人惊讶的是对于Locale.getDefault()这些工作 mvn test -DargLine =“ - Duser.language = de -Duser.region = DE” 而不是 mvn test -DargLine =“ - Dsystem.user.language = de - Dsystem.user.region = DE“ – bibstha 2012-03-21 14:27:41

12

我与Surefire插件经历过。 Surefire插件在由Maven启动的另一个JVM实例下运行。命令行参数可在pom.xml中的surefile-plugin配置下进行配置。这是我们配置的一个例子。

 <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> 
2

小心不要混淆配置文件与命令行参数。配置文件(pom.xml)覆盖所有的cmd参数。所以如果你想通过命令行传递它,像raisercostin解释的那样,不要在pom.xml中配置surefire插件。