java - Maven:UTF-8 中的源编码不起作用?

标签 java maven encoding utf-8

我正在将项目从 Ant 转换为 Maven,并且在处理 UTF-8 字符的特定单元测试时遇到问题。问题在于以下字符串:

String l_string = "ČäÁÓý\n€řЖжЦ\n№ЯФКЛ";

问题是单元测试失败,因为String被读取为如下:

?äÁÓý
€????
?????

java 类保存为 UTF-8,我还在 pom.xml 中指定构建编码为 UTF-8。

这是我的 pom.xml 的摘录:

...

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

...

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
            <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
    </plugin>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.15</version>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-report-plugin</artifactId>
      <version>2.15</version>
    </plugin>
 </plugins>
</build>

我在这里错过了什么吗?如果有人可以在这里帮助我,那就太好了。

更新

关于测试代码:

@Test
public void testTransformation()
{

    String l_string = "ČäÁÓý\n€řЖжЦ\n№ЯФКЛ";
    System.out.println( ">>> " + l_string );
     c_log.info( l_string );
    StringBuffer l_stringBuffer = new StringBuffer();
    int l_stringLength = l_string.length();

    String l_fileName = System.getProperty( "user.dir" ) + File.separator + "transformation" + File.separator + "TransformationMap.properties";
    Transformation.init( l_fileName );

    Properties l_props = Transformation.getProps();
    for ( int i = 0; i < l_stringLength; i++ )
    {
        char l_char = l_string.charAt( i );
        int l_intValue = (int) l_char;
        if ( l_intValue <= 255 )
        {
            l_stringBuffer.append( l_char );
        }
        else
        {
            l_stringBuffer.append( l_props.getProperty( String.valueOf( l_char ), "" ) );
        }
    }
    c_log.info( l_stringBuffer.toString() );
    byte[] l_bytes = l_string.getBytes();
    byte[] l_transformedBytes = Transformation.transform( l_bytes );
    assertNotNull( l_transformedBytes );

}

以下逻辑并不真正相关(?),因为在第一次 sysout 之后,前面提到的“?”打印而不是正确的字符(因此以下测试失败)。也没有使用默认平台编码。

测试根据TransformationMap.properties文件对每个字符进行转换,格式如下(摘录):

Ý=Y
ý=y
Ž=Z
ž=z
°=.
€=EUR

需要注意的是,当我用 Ant 构建项目时,测试运行没有任何问题。

最佳答案

我自己找到了一个“解决方案”:

我必须将编码传递给 maven-surefire-plugin,但通常

<encoding>${project.build.sourceEncoding}</encoding>

没用。我仍然不知道为什么,但是当我将命令行参数传递给插件时,测试会正常工作:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.15</version>
      <configuration>
        <argLine>-Dfile.encoding=UTF-8</argLine>
      </configuration>
</plugin>

感谢您的所有回复和补充意见!

关于java - Maven:UTF-8 中的源编码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17656475/

相关文章:

java - 在 InheritanceType.JOINED 中使用 AttributeConverter 映射 MySQL ENUM 在 Hibernate 5.3.6 (JPA 2.1+) : No enum constant 上抛出 IllegalArgumentException

java - 如何将 UriInfo 对象注入(inject)到非资源类中

java - 连续执行不同的Completables

bash - 是否可以从 Maven 运行 Bash 脚本?

python - Python readline()方法导致UnicodeDecodeError

node.js - golang base64 编码 vs nodejs 缓冲区 base64 编码

java - 如何看懂java String源码

java - 如何使用 MVN 构建 hortonworks/simple-yarn-app

java - maven tomcat插件配置

android - Bixolon sdk编码characters_android