2012-01-17 98 views
10

我在maven-surefire-plugin and default locale中讀到Maven運行測試分叉並因此可能會丟失您可能已設置的任何語言環境。在Maven pom文件中設置surefire插件語言環境

有沒有辦法在分叉模式下在Maven中運行測試並仍保留語言環境?

- 編輯 -

所以,要澄清一點:這是完全有可能利用設置在系統屬性的語言和區域:

<systemPropertyVariables> 
    <user.language>en</user.language> 
    <user.region>GB</user.region> 
</systemPropertyVariables> 

而且他們實際上是傳遞給正在運行的進程。但是,這不會相應地設置區域設置;區域設置保持爲系統默認值。

+0

您是否嘗試過設置'user.country'而不是'user.region'? – 2012-01-23 13:42:25

+0

設置國家不起作用。它將它設置爲系統屬性就好了,不會以任何方式影響Locale。 – JHollanti 2012-01-24 09:49:27

+0

Sun的文檔[1]指出: 「第二,在某些Java運行時實現中,應用程序用戶可以通過設置user.language,user.country和user來在命令行上提供此信息來覆蓋主機的默認語言環境。變體系統屬性「。 那麼,您使用的是哪種JVM? [1] http://java.sun.com/developer/technicalArticles/J2SE/locale/ – 2012-01-24 11:02:35

回答

2

我沒有辦法來測試這一點,但不妨一試:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.9</version> 
    <configuration> 
     <project> 
      <properties> 
       <user.language>en</user.language> 
       <user.region>GB</user.region> 
      </properties> 
     </project> 
     <includes> 
      <include>**/*Test.java</include> 
     </includes> 
     <forkMode>pertest</forkMode> 
    </configuration> 
</plugin> 

編輯:OK試試這個:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.9</version> 
    <configuration> 
     <systemPropertyVariables> 
      <user.language>en</user.language> 
      <user.region>GB</user.region> 
     </systemPropertyVariables> 
     <includes> 
      <include>**/*Test.java</include> 
     </includes> 
     <forkMode>pertest</forkMode> 
    </configuration> 
</plugin> 
+0

不,不起作用。甚至不設置系統屬性。 – JHollanti 2012-01-24 09:48:40

+0

好的,新的配置張貼 – 2012-01-25 15:51:37

+0

這一個設置系統屬性就好了。區域設置保持默認。謝謝你的努力。 – JHollanti 2012-01-26 11:56:52

2

應用程序的缺省地區有三種方式確定。 第一個,除非您明確更改了默認值,否則getDefault()方法返回Java虛擬機(JVM)首次加載時最初確定的地區設置 。也就是說,JVM確定主機環境中的默認語言環境。主機環境的語言環境由主機操作系統確定,並由用戶在該系統上建立的首選項決定。

,一些Java運行時實現中,應用程序,用戶可以通過 通過設置user.language,user.country提供有關 命令行信息覆蓋主機的默認語言環境,並 user.variant系統屬性。 [Source]

我認爲你是第一部分的受害者,所以第二從來沒有得到一個機會。

相反,你可以做的是在你的單元測試(或者一個基類中,)設置默認語言環境編程爲同一文本後說:

三,你的應用程序可以調用setDefault(Locale aLocale) 方法。 setDefault(Locale aLocale)方法可讓您的應用程序 設置全系統資源。在使用此方法設置默認區域設置後,對Locale.getDefault()的後續調用將返回新設置的 設置區域設置。

static{ 
     Locale.setDefault(Locale.UK); 
} 
+0

是的,那肯定會起作用。唯一的問題是我有大量的測試。而且,將來有人可能並且最肯定會編寫一個單元測試,而無需以編程方式明確設置區域設置。 – JHollanti 2012-01-26 01:00:32

12

試試這個:

<plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <configuration> 
      <argLine>-Duser.language=en -Duser.region=GB</argLine> 
     </configuration> 
    </plugin> 
+0

這解決了我的問題。謝謝! – sebadagostino 2015-04-28 15:18:48

0

我有同樣的問題,但我不得不解決它沒有 ingerence到pom.xml文件。這可以通過Maven的全局配置文件(通常位於~/.m2/settings.xml)。要做到這一點,你添加一個像下面的配置文件,將默認激活:

<?xml version="1.0" encoding="UTF-8"?> 
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
     http://maven.apache.org/xsd/settings-1.0.0.xsd"> 

    ... 

    <profiles> 
     <profile>  
      <id>my-prof</id> 
      <properties> 
       <argLine>-Duser.language=en -Duser.region=GB</argLine> 
      </properties>    
     </profile> 
    </profiles> 

    <activeProfiles> 
     <activeProfile>my-prof</activeProfile> 
    </activeProfiles> 

    ... 

</settings>