在Maven pom文件中设置surefire插件Locale

时间:2012-01-17 21:21:22

标签: maven locale maven-surefire-plugin

我在maven-surefire-plugin and default locale中读到Maven运行的测试分叉,因此可能会丢失您可能设置的任何语言环境。

有没有办法在分叉模式下在Maven中运行测试并仍然保留区域设置?

- 编辑 -

所以,澄清一点:完全可以使用以下方法在系统属性中设置语言和区域:

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

它们实际上已传递给正在运行的进程。但是,这并没有相应地设置区域设置;区域设置仍为系统默认值。

4 个答案:

答案 0 :(得分:18)

试试这个:

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

答案 1 :(得分: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>

编辑:好的,试试这个:

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

答案 2 :(得分: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);
}

答案 3 :(得分:0)

我遇到了同样的问题,但我必须解决没有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>
相关问题