19

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

有没有办法在 Maven 中以分叉模式运行测试并仍然保留语言环境?

- 编辑 -

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

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

它们实际上被传递给正在运行的进程。然而,这并没有相应地设置语言环境;区域设置保持为系统默认值。

4

4 回答 4

23

尝试这个:

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <argLine>-Duser.language=en -Duser.region=GB</argLine>
        </configuration>
   </plugin>
于 2013-07-26T15:06:50.907 回答
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>
于 2012-01-23T16:45:03.417 回答
2

应用程序的默认语言环境由三种方式确定。 首先,除非您明确更改了默认值,否则 getDefault() 方法将返回最初由 Java 虚拟机 (JVM) 首次加载时确定的语言环境。也就是说,JVM 从主机环境中确定默认语言环境。主机环境的区域设置由主机操作系统和在该系统上建立的用户首选项决定。

其次,在某些 Java 运行时实现中,应用程序用户可以通过在命令行上设置 user.language、user.country 和 user.variant 系统属性来提供此信息,从而覆盖主机的默认语言环境。[来源]

我认为你是第一部分的受害者,所以第二部分永远没有机会。

相反,您可以做的是在您的单元测试(或者可能是其基类)中以编程方式设置默认语言环境,如稍后在同一文本中所述:

第三,您的应用程序可以调用该setDefault(Locale aLocale) 方法。该setDefault(Locale aLocale)方法允许您的应用程序设置系统范围的资源。使用此方法设置默认语言环境后,后续调用Locale.getDefault()将返回新设置的语言环境。

static{
        Locale.setDefault(Locale.UK);
}
于 2012-01-25T16:00:35.270 回答
1

我有同样的问题,但我必须在不影响文件的情况下解决它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>
于 2018-01-13T20:38:07.853 回答