【问题标题】:Set surefire plugin Locale in Maven pom file在 Maven pom 文件中设置surefire插件语言环境
【发布时间】:2012-01-17 21:21:21
【问题描述】:

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

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

-- 编辑--

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

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

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

【问题讨论】:

  • 您是否尝试过设置user.country 而不是user.region
  • 设置国家无效。它将它设置为系统属性就好了,不会以任何方式影响语言环境。
  • Sun 的文档 [1] 指出:“其次,在某些 Java 运行时实现中,应用程序用户可以通过在命令行上设置 user.language, user 来提供此信息来覆盖主机的默认语言环境。 country 和 user.variant 系统属性。”那么,您使用的是哪个 JVM? [1]java.sun.com/developer/technicalArticles/J2SE/locale
  • 用 Java 5、6 和 7 尝试过。再一次,user.language、user.country 和 user.variant 被传递给进程就好了。区域设置保持默认。

标签: maven locale maven-surefire-plugin


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案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>
    

    【讨论】:

    • 这个设置系统属性就好了。区域设置保持默认。不过感谢您的努力。
    【解决方案3】:

    应用程序的默认语言环境由三种方式确定。 首先,除非您明确更改了默认值,否则 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);
    }
    

    【讨论】:

    • 是的,这肯定行得通。唯一的问题是我有大量的测试。而且,将来有人可能并且肯定会编写单元测试而不以编程方式显式设置语言环境。
    【解决方案4】:

    我遇到了同样的问题,但我必须在不影响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>
    

    【讨论】:

      猜你喜欢
      • 2019-06-07
      • 2013-06-04
      • 2014-03-05
      • 1970-01-01
      • 2012-12-11
      • 2021-02-11
      • 1970-01-01
      • 2011-01-24
      • 2010-11-19
      相关资源
      最近更新 更多