2009-05-05 94 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插件。