2012-01-17 160 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>